Boolean function example (C++ programming tutorial)

Sharing buttons:

alright welcome back in this video I'm

going to make an example of a function

that returns the boolean type so I think

the idea that I'm going to do in this

video is we're going to make a function

that checks to see if someone has

inputted a four digit PIN number we

don't need to do this outside of the

main function but the point is just

getting used to the idea of making

functions that do perform tasks for us

outside of the main function and in this

case we haven't done a boolean type yet

so that will be this one so let's start

off with the function outside of the

main function place it will be a boolean

type and it's going to check if a pin

number is four digits that someone will

eventually type in in the main function

here so let's call it something simple

like is four digits and we're going to

pass in actually a string and we'll call

that pan and the reason we use string is

because we can really easily check the

length of a string we don't need it to

be an integer or something like that

because we're not going to be doing any

sort of arithmetic operations on this

person's pin number alright so we're

going we are going to return a boolean

variable our boolean type so let's set

up our boolean variable that will

eventually return and we'll call that

status and let's initialize that to true

so now we're going to say is just simply

with an if statement if pin this

incoming pin that the string that

someone is passed in if pin dot length

is not equal to four like that then

we're going to do something and that

thing that we're going to do is simply

is just we're going to change status to

false boom there we go and then once we

have that make sure we got that

semicolon in there and then go ahead and

we're just going to return status okay

so basically what this function is doing

is you will write everything else in

here in one second but it's accepting a

pin number from the main function and

it's checking to see if it's four digits

long or not and so if it if it is four

digits then nothing will change and

we'll return status which we've already

set to true and if it's not four digits

then we're going to come in here set it

to false and we're going to return false

now if you recall

boolean operators zero is false and one

is true there's only two types of only

two things that can represent all right

so let's go ahead and make our main

function here so I guess first of all

we'll need that string sure let's call

it pin and then we'll ask our user to to

input something so we'll see out enter a

four-digit number

all right and maybe just let them have

entered they're on the same line just

like that and then let's see in what the

user input the pin number all right now

what we need to do is we're we could we

don't need to have like we don't need to

write this whole function out here like

we could very easily do this with a like

an if-else statement but the point is

we're just getting used to the idea of

sending some work out to functions that

aren't inside the main function so with

that said we will be using an if

statement so we're going to say if is

four digits and we're passing in pin so

we say if this is equal to one so if

this is true then do something and so

we're going to see out and accepted just

like that alright and then else we're

going to do something else

so else C out diplo Aereo

C out and gain ID all right something

just like that we don't actually need

these curly braces in here because these

are just one line statements so we can

go ahead and get rid of those and then

lastly we just want to see out and line

either way okay so let's build and run

this see if we got little semicolons in

there probably not

looks like it might be working all right

so let's just run this program and see

what happens and we'll talk about what

we're seeing so here it's saying enter a

four-digit pin

so we'll 1 2 3 4 that is 4 digits and

there when we hit enter this is pin

acceptance that's pretty cool that's

actual we're hoping for it to do let's

do something else and put in three

digits pin denied and let's put in

something else 5 digits and then hide

well there you go that's pretty cool so

let's if we can if we want we can talk

about what's happening here so until the

main function after we ask the person to

input a number into pin they enter

something and if you've seen that can

enter four digits or I can enter five or

three or something now in the if

statement we pass it into we pass pin

into is digits and this is going to

check to see whatever the result is or

the output to see if it's equal to one

now when we pass in pin it gets changed

to lowercase pin here and that's what

we'll call it for the body of this

function and here we have yeah we're

just checking full status we'd already

defaulted the true so the only way it

can become false is if if this statement

is satisfied so if it's anything but

four digits long and there you get there

you see we're either returning status as

a 1 or a 0 0 for false 1 for true and

then when we return that it kicks us out

here so say that it was true it's going

to return a 1 and then if the output of

this say 1 is equal to 1 and that's why

we're seeing out pain accepted otherwise

we are returning a 0 which is a false

and it's saying pin denied so there you

go you don't have to like we could have

easily gotten rid of this whole function

just in something clever at this if

statement but because this is we're just

learning how to make functions there you

go if you want just outsource it you

know to its own function here of the

boolean type then that's an example of a

boolean function that you can use to do

something productive alright see you in

the next video