Why we want to do that essentially it’ll just make sure that everything works correctly so we want to make sure that we define this function before we actually call it down here and get a value back so the first thing that I need to tell C when I want to create a function is what type of data I want to return if you’ve been following.
Along with this course you’ll know in the last tutorial we use the void return type which basically means we weren’t going to return any information but now we want to actually return information so we want to Cuba number so why don’t we return a double and you can put any.
Datatype here you want you can also put like int or character or character array but let’s just do double and we’re gonna give this a name so I’m gonna call this cube and I’m gonna make an open and closed parenthesis and inside of this parenthesis we’re gonna allow this method or we’re gonna allow this function to accept one parameter as input so it’s just going to be another double and call it.
Num so basically what we want to do is we want a cube.
Num and we want to give that value back to the user so over here I’m just gonna create a variable it’s just gonna be another.
Double called result and I’m gonna set it equal to num times num times num so basically this variable result now represents or now stores the cubed value of num all we need to do to return this value now is just say return result and this return keyword is basically gonna do a couple things the first thing it does is it breaks us out of the function so whatever we put that return keyword it’s gonna break us out of the function.
And it’s gonna return this value back to the caller so down here we can actually call this function so actually what I want to do is I want to print out the answer that we get back so I can do printf and in here I’m just gonna say answer and we’ll print.
Out the answer so percent F so I’m using.
% F because we’re expecting to get a double back from here remembered double is a return.
Type and over here I can just say cube and we’ll pass in a number let’s pass in 3.0 essentially what’s gonna happen now is we’re going to call this function all this code is going to execute and this function is going to get a value back basically the value of cubing 3.0 so normally like in the past in this course what we wanted to print on a number we’d have to put like.
4 or 76 or you know whatever we’d have to put.
A number right there but we can just call this function because eventually this is going to get a number back anyway so this will contain a number after the functions done being called so I can actually just run my program now and this is going to print out the result of cubing 3.
Here we get 27 point 0 0 so 3 cubed is 27 3 times 3 is.
Like our cube function works let’s try with another number why don’t we do 7 so let’s run this again and we get 343 I’m guessing that’s right so basically what we did is we created this function and we use this return keyword and we returned back to the caller the value of cubing the number and that’s really cool and actually we can make this a lot simpler so I could actually just get rid of this result variable and I.
Just straight up return num cubed and this is and this is gonna do the same exact thing so we should get the same answer yeah so this can be really awesome and one thing I want to point out is.
This return keyword will actually break us out of the function so if I came down here and I said like printf and I printed out like here this actually.
This code is actually never gonna get executed so if I was to run my program you’ll notice that it’s not printing out here so that never gets.
Printed out in other words this line of code down here print out here this never gets touched because when we use this return keyword this breaks us out of the function so whenever C sees this return keyword it just exits the function and goes back down here to the normal program so just keep in mind that you can’t really put any code after you use this return keyword because it will break you out of the function and actually want to talk to you guys about one.
More thing really quick you’ll notice up here I’m creating my cube function above the main function so I actually created this above here and the reason that I did that was because if I was to move this down here below the main function and I try to run my program you’ll notice that we’re getting an error over here.
And actually if we look at the error down here in the output it says error conflicting types for cube now essentially what’s happening is when I create this function down here below this main.
Method when the main method over here tries to call it it actually doesn’t know about this function yet like it doesn’t necessarily know like what this function is what it’s going to return and what parameters it’s gonna take because we created it after we created this main method so what I.
Can do is actually something called prototyping and when I prototype it’ll allow me to create this function below this main function without getting this error and basically when you prototype I’m basically just going to write out the function signature so we would call this like the signature basically the return type and the parameters and the name so if I was to put this up here and put a semicolon now when I.
Run my program you’ll see that we’re not getting this error anymore and we’re actually getting the answer back so this is a way that you.
Can create functions below that main function without having any problems and you know basically any functions that I created I could put another prototype up there at the top and it’s gonna be fine in this tutorial and talked to you guys about if statements in C an if statement is a programming structure which we can use to help our programs to make decisions by using an if statement our programs can actually respond so in certain circumstances they can do certain things and in other circumstances they can do.
Other things and if statements are extremely powerful and they’re a great way to add some intelligence to our programs for the purposes of.
Gonna be building a function so we’re gonna build a function and it’s gonna be called the max function basically what this function is gonna do is it’s gonna take two parameters two numbers and it’s gonna tell us which number is the biggest so I could give it like four and I can give it a ten and it’ll tell us which number is bigger let’s go ahead and do that I’m gonna make this function up here and it’s.
Going to return an integer and I’m just gonna call it max and it’s gonna take as parameters two integers so it’s gonna take an integer num one and it’s.
Gonna take another integer num two and now we’ll just make the actual function body so inside this function our job is to figure out whether num one is the biggest or num two is the biggest and we’re going to return the biggest to the caller so the first.
Thing I’m gonna do is create a variable and it’s gonna be.
An integer and I’m just gonna.
Call it result and I’m not gonna.
Give it a value right away what we need to do in this function is we need to figure out which one’s bigger we need to figure out if num one is the biggest or we need to figure out if num two is the biggest and this is a perfect situation to use an if statement an if statement allows us to check a certain condition and if that conditions true we can do one thing and if the conditions.
False we can do another thing so down here I’m gonna use an if statement I’m basically just gonna say if and then we’re gonna make an open and close parenthesis and I’m gonna make an open and closed curly bracket now inside of here inside.
Of these parentheses we need to specify a condition this is something that’s either gonna be true or false if the condition inside.
Of these parentheses is true we’re gonna execute the code inside of these curly brackets if it’s false then we’re just gonna move on so in order to tell whether or not num1 is bigger than num2 or num 2 is bigger than num 1 we can actually compare them so.
Down here I could say if num 1 and.
I can use this greater than sign num2 I’m basically saying if num 1 is.
Greater than num2 then I want to do something so down here we can set result equal to num 1 if this.
Condition is true then we’re gonna do this so only when num 1 is.
Greater than num2 are we gonna set result equal to num 1 so basically by using this if statement we’re able to tell which one is bigger but here’s the thing what if num 1 isn’t greater than num – what if num 2 is greater than num 1 well I can use something called an else statement so I can come down here and I could say else and I don’t actually need to use an open and close parentheses I can just make an open.
And close curly bracket and.
The code inside of this else block is going to execute if the condition up here is false so if this condition is false if num 1 is not greater than num 2 then we can just set result equal to num 2 and then down here finally we’re gonna return result.
So depending on whether or not this is true result is either gonna set equal to num 1 or it’s gonna be set equal to num – so let’s go down here into our main method and why don’t we call this so I’m actually gonna print out the result so I’m just gonna say printf and we’re gonna be.