Welcome back guys.
So now we're going to actually learn how to correct functions and I should explain functions a little
more because I think I think it's more important to learn about functions before I teach you about data
types such as integer and Char functions are kind of required first really.
So what I'm gonna do.
I want you to copy what I'm doing.
We're gonna go void my fund LLOYD
Okay so let's explain this.
My Funk is a function.
This function doesn't take any function arguments.
So we put a void in here.
Now what you can do if you go back into Maine I can I can call my funk like this and what will happen
is our program when it goes into the main function it will call my funk which will start running code
in here.
So if I went in here and I did print f my funk called and put a semicolon at the end because in C and
all lines of code with a semicolon and everything between these brackets here is the code that gets
run when you call the function.
So obviously this is the name.
This is the return type and inside here goes the function arguments.
Now avoid return type means when not returning any value.
That's what this means.
So when you call my func and the return type is void nothing is returned.
So there is no need to have return.
There's no need to have this but if you did want to use the return keyword to return from my func earlier
than you could you would just go return like this.
There's no need to pass in a value or anything like that because it s of a void return type which means
no values returned.
And if I was to have a print here you don't have to do this.
Just an example.
This would never be called because we print out my phone called and then we return from the function.
So that print f test never gets called and there are reasons for when you would use this such as returning
from a function early based on a condition of some kind.
Okay.
If we now go back to command prompt and once again you need to go to the Hallowell app directory and
we open up CMT for command prompt we type C D.
Right click to paste press enter.
And now we can compile our program again.
Suggest to GCSE main dot c show main press enter our program is now compiled and now if you type Main
and press enter you can see my fund called Hello World and you'll notice that they're all in the same
line.
What if you wanted a separate line.
Well there's a special way to do that and see you just do this.
So this character here means that you're gonna use some sort of special character code of some kind.
So by doing this.
And that means new line whereas if you did R instead of N that is a carriage return.
So quite commonly you'll see r n so carriage return and then a new line but.
And we'll just be fine for now.
It's fine in most cases.
So if you do the same here and we go back up press the Empire twice to come back to your compile Command
press Enter GCSE compiler right now we're on and then do Main and we can see them now on separate lines.
So that's how you create new lines in C use this.
So let me explain.
Another thing that we can do let's make a new function is going to return an integer which is a number
by the way integer is a number.
And we are going to call it my sum and we're going to have an argument here integer a.
And then another argument integer B.
So while they still do these will add them both together and return the result.
So how do we do that.
Well you do return a plus b.
So just like mathematics on a calculator where you might do 20 plus 30 here we do a plus b.
So what will happen is when someone calls my sum they passing a value here and a value here and then
it gets added together and it returns the result.
So let's now get rid of this line here my func and we're going to go my some five and five.
So while this does five goes into a as we did here and then this five goes into B.
Just to simplify a second to change to attend.
Explain this a little better.
So 10 goes into a five goes into B and how that works is it line is in line with these arguments.
That's how it works.
So we can see my son takes an integer so we pass in ten and it also takes another integer so we pass
him five.
Now we can see we're not store in this result anyway so let's now store that result.
So if we go integer result equals my sum 10 5.
Now what have we done we have created a variable now variables in C allow you to store information.
So in our case we've created a variable called result and result will store a number.
Now decimal points cannot be in integers you use doubles for that or floats but that'll be explained
more later.
Now if we now run that program nothing would happen because it would some together but we don't output
the result.
So how do we do that.
Well let me show you.
Let's get rid of this.
We're going to go print f result equals.
Now the result is I'm going to do result is so that you don't get confused thinking that the equals
is required.
Result is and you do.
I percentile and then you do the new line.
So what happens here then.
Well currently undefined behavior because we haven't defined the result we haven't supplied the result
but what will happen is it will say the result is an 10 percent.
I takes from an argument that we provide here.
If we do results when we press control less to save.
So it says result equals my sum 10 5 which calls this function we add a and b together which is in our
cases 10 and 5.
The result is returned and stored in the result variable which is an integer a number and then we output
the result by saying result is percentile.
Now one that one the one the program sees percent ie it looks for these arguments passed to us here
and they have to be provided in line with the arguments provided.
So for example if we just change this a little bit more I want you to do instead a result is we're gonna
get rid of this now because I just need to explain that to you.
So we're going to say I plus i equals I.
Now here we provide 10.
And here we provide five.
And what this will do is the operating system comes in.
It runs our program it sees percent I.
So then we pull from the first argument here which is 10 and then we output a plus symbol.
Literally this can be this could even be plus just to show that they're unrelated.
And then it finds another percent eye.
And it puts five in there and then finally it outputs an equals sign and then it has a percent eye again
and it outputs the result.
So if we can pull out now go back to the program up it twice and turn up it once or twice maybe 10 plus
five equals fifteen.
So that's how the functions work.
So essentially you define a return type which is this you define a function name which is that and you
define arguments in between these brackets everything between these curly braces is the code this run
when this function is called.
So you can think of functions as ways of splitting up a program into different pieces.
They're an essential part of C and they're very important and they allow you to prevent repeating yourself
in code which is a very important thing for a clean code base so that is how you define functions.
But let's explain a little more now.
What happens if main is at the top.
Well there'll be a compile error if you go back here when that again implicit declaration of function
my sum.
OK.
Why is that then.
Well because these are defined below.
So when the compiler comes in and it goes to call my son my son hasn't doesn't exist yet because that
the compiler hasn't seen it yet because it's below it.
So how do you get around that problem.
Well you define what's known as a prototype.
Few if you copy this line here paste it here and just put a semicolon at the end.
So what this now says is it says to the compiler.
This is a prototype for my sum.
This is what it will look like.
I will define it later.
I don't really care about right now but you need to be aware of it for later.
That's what it says.
Now if we go to compile it now you can see it compiles fine because we've let the compiler know that
we will be declaring this later on.
Now this is how the head of files work.
So these function prototypes exist in the head of files so that when you include this it will have a
bunch of these four prints.
It will be like that for example.
And when we call print f hasn't it it doesn't actually.
It's not defined like this with curlies and everything is just a prototype like this which basically
means this exists somewhere we don't know where it exists yet but just make a note of it because we're
going to be using it.
That's all the prototypes do.
And again a very important part of C so that is function prototypes.