Hi, guys.
What's up?
So today we will learn a new concept called function.
So we're now functions so mean is a function.
OK, so this is was.
Now, boss has many work to do.
So what it will do, it will you call it simply?
Call.
Called.
And call.
So my main function is a board.
It has many work to do.
What it will do?
It will call its many employees.
For example, my book is to find Squirter phone number.
Squared off?
No.
My work is to find squared over.
No.
So what do it mean two men will make.
Meanwhile, call a function I employ and we'll say, hey, find this good of a number.
And after finding this good of a number, it will get it done.
That is aired.
Dodie's mean.
Similarly, suppose.
The board wants to find vetted arguin number is ARED, or even so, it will call its employee and after
calculating the deserted, the employer will report to the mine.
So employee will return I these mean.
Similarly, if mean, it has to do some work, for example, if he want to check whether the given number
is prime or not.
So, I mean, who wants to check with a given number is prime or not?
So it will assign this work to an employee and that employee, after completing the work will report
or you can say it done to the mean and finally mean.
We're done.
The answer.
Towards higher authority.
OK.
So all these things are a function.
So these employees are functions.
Function, function and function.
OK.
So Maine has many work to do.
Maine is a very busy person.
He has a lot of work to do.
He will make functions, he will call functions, and the function will do the work and they will return
answer to the main.
OK.
So why we need function.
Number one, our code becomes very organized.
OK.
So what we were doing delo we were writing our whole code inside the mean.
We were writing our whole code inside the mean.
So that was not organized to.
What we will do now is we will create function suppose function when function too.
And so on and from mean I will call those functions.
So call function when.
Similarly if there is a need to call function two I will call function to.
Our code becomes very organized.
Number two, which is most important, our code becomes reuseable, our code becomes reuseable.
For example, I have to check for five numbers whether they are even ordered or so I have five numbers.
Let's say one, two, three, four and five.
I have to check whether the numbers are prime or not.
What do I do?
I will call this function five times.
Similarly, if I have to check.
There are five numbers.
If I have to check whether the numbers are even a order, I will call this function five times.
So we will write the code once.
We will write code once.
One time.
We will write code only one time.
And we will use this code.
Many times.
OK.
So our code becomes reusable.
You just have to write the code one time.
And if there is a need, we can always call that function.
OK, third is debug, debug means finding the error.
So finding error also becomes easy.
How?
Suppose.
Initially.
Until now.
What we were doing, we were writing our whole code inside the mine.
So suppose our code is off Blurred Lines.
OK.
And now what are doing?
We are creating function, function, one function, too.
And I mean, suppose now there are only ten lines in mean and there are only five lines here.
And suppose to lines here.
So if I have to find error, first of all, I will check.
Is my function to correct a function to correct.
OK then I will check a function one.
Correct.
OK.
Now there is a problem in this 10 lines.
So I would debugging.
Debugging means finding at a finding error becomes easy, finding error becomes easy.
OK.
So there are three main benefits of using function.
Reusability is the mean.
So reusability is the main advantage of using functions.
OK.
So let's see how function works.
So here I am having hashing, glued I stream using namespace as deeding.
Now let us create our first function.
So void.
Brent.
Hello.
See out.
Hello.
And now this is what I mean and mean, so our task is to print hello.
So what do they do?
I will call this function print.
Hello.
OK, so what will happen?
My task is to print hello?
So what I will do.
I created a function.
The name of the function is print.
Hello.
And from mean, I will call this function.
OK, so at this line, I am calling I am calling this function.
Brent, hello.
So what Brent does, it will.
Brent.
Hello.
No, it is not doing anything.
It is just printing.
Hello.
OK, now let's see one more problem.
Suppose our aim is to add two numbers.
Our aim is to add two numbers.
So what I will do?
And.
Ed.
Ed is the name of the function.
We have to add two numbers.
So what do we do?
We will give function what other two numbers that you have to add.
OK.
Suppose the first number is a.
And the second number is B.
And you have to add them, so end answer equals A plus B.
And now you have to return this answer to the mean man is the boss.
You have to return this answer to the main.
I return answer because I do mentally, we have to report to mean meanest boss.
So Intiman, our aim is to add two numbers.
So I will take two numbers as input and for finding the submission.
I will call it a function.
So Ed and I have to give add two numbers, add a comma maybe.
OK, now this thing is wrong.
So at this line, what I'm doing, I'm calling the function Ed.
And Ed is taking Tuvalu's as input.
Well, I'm giving Tuvalu's as input and I will lose out in danger.
That is why I have written and paid this calculating the answer.
A plus B.
And then it is returning the answer.
So it is returning the answer to the mean.
So this is call and this is done, but against a report.
Two men.
So I am reporting integer.
So I say that integer, I am reporting an integer.
You are not storing that integer.
You have to store that integer.
I'm not giving you anything.
So you have to store them.
So what I will do, I create a variable.
Let's say the name of the variable is see.
So end sequel's add.
Now this is correct.
OK, so add function was returning answer and we have to store that answer.
Now, what is main function returning it is returning an integer.
And that's why I have written in here.
OK, in the above example, in the above example, I have written void here, because this printhead
function is not returning anything to the mean.
OK, so the first thing.
So the meaning of this thing is what you will return to the mean.
OK.
So what you will have done.
So in this case, I was not returning anything.
So I have written void here because I am not returning anything.
But in this case, this ad function have to return an integer that is uncertain.
So I have written enter into means you will return an integer toadie mean.
So in means you will return this function.
You means this function.
This function.
Add this function.
Add will return an integer.
And indeed your value to mean.
Or let's say calling function mean is calling function.
So an integer value do calling function.
So who is calling is mean is calling Ed.
OK.
So in previous case, since this function was not returning anything.
So I am not distorting it because it is not pretending anything.
But here in this case, I am returning an integer.
So we have to write in C equals Ed because this add function will are done.
And ENDESA, that is answer.
And when we get down to it, I will right.
See you out.
See?
OK.
So, for example, I want to find.
They're squared off.
No, I want to find that squared of a number.
So what would I have done?
I will have done and indeed it.
My aim is to find squared off.
No.
So what?
I will return.
I will return in danger.
Let's say the name of the function is square.
The name of the function is square.
What will it take?
It will take an input.
It will take an integer who's a square.
We have to find.
So it will take an integer, let's say, and a.
And we have to find the square of a.
So end answer equals a multiply by a.
OK.
And after calculating the answer, we have to return this answer.
Now, from the mean I will call squared function.
So the vogue of the main was to find the square of a number.
And for that we have made a function.
So we'll take input.
Who's number, who's a square?
We want to find out.
Suppose the number is.
And I want to find this square off.
And so I will call function.
Pasquier function pick.
And as input and find the square of this number.
And this function will return me an integer value.
Which is the square of the given number.
So since this function is returning something, I will have to start it.
So end let's say B into B equals this.
OK.
Why end.
Because this function is returning.
And.
And finally, I can see out be OK.
So we call this thing as a return type.
So this is a done type.
I am returning and deja here.
We call this thing as name of dysfunction.
So this thing is called.
Name of function.
Now, the values that it is taking is called arguments, the values which in my afunction will take
as input.
I've called arguments or against, say, barometer's.
So this thing is called calling the function, we are calling the function square.
And so this is calling function.
OK.
And this whole thing is called this whole thing is called definition of function and definition of function.
OK, so have you noticed one thing?
So if I want to call a function, that function was defined above.
Similarly here, I want to call it a function.
So add function was defined above mean.
And similarly here, if I want to find the square, if I want to call a function square, so squared
function is defined above the mean.
So village to an important result that if our function, if a function.
Let's call this function as a safe, a function vaunt to call it a function, wants to call and enter
function B in a function, wants to call other function B, then B must be declared above A.
Above function a otherwise, what will happen?
We will get completion at it.
We will get completion.
And now VI we will get completion at it.
So the logic is very simple.
Let's take this example.
So I told you compiler will start compiling from top to bottom.
So as soon as the come by level reached this line.
So you are saying, hey, call function squared, so compiler knows that, hey, I have compiled the
function squared, so squared exist, hence it can call.
But if you have done so, if you have written mean.
And then you have done, let's say, add function.
And here you have Korps and the air, you have called this ad function.
So Compiler will start compiling from top to bottom.
It will reach at this line.
At this line.
You are calling ad function.
Compiler will say, I don't know.
I don't know what this ad.
Okay.
The compiler will give us error.
I don't know what this ad.
So that is that is an if a function, a wants to call.
And at their function, B, then B must be above.
Function B must be about function.
A.
OK.
Now, one last thing.
So there are three things.
So the first thing is.
What is a declaration of function?
Second is definition of function.
And third is calling function.
OK, so let me first tell you what his definition calling.
Suppose I want to add two numbers.
What I would do and add it all day to number as input and be our parameters here.
And it will at Dundy onset.
So they've done.
A plus B.
OK.
And suppose this is your main function and main function is calling this ad function.
So let's say and sequels call ad function, give two and three as input and then see out.
See.
OK, so this line is called calling we are calling function and.
OK.
And since we have right the definition here.
So this is called definition.
OK, so, for example, I am writing in bed for this at a function will take to name, but as input.
Now I leave it here.
I leaving, I'm leaving it here.
And now I am writing Intiman this main function well call function add will give you a number as input.
Brent, that is reserved and know the definition.
So entered into a coma and to be.
Done.
A bless me.
OK, now, welcome, Pilar.
Give me an hour.
If I write this code.
No, because compiler will compile from top to bottom.
It will reach and stop at this line.
It will say, hey, I'm calling ad function.
So Compiler will say, have I compiled something called ADD?
Yes, I have compiled something called add.
Now word compiler will do.
It will search various ad.
Now the compiler will reach here.
Oh, here I am having add.
It will calculate the result and it will return.
That is old.
OK.
So this thing is called declaration of function.
I am declaring that I will.
I am telling the compiler.
Declaration means you are telling the compiler.
Hey compiler.
I will be writing and function.
So please don't give me at it.
I will write a function later.
Combine I will say.
Okay.
As you wish.
This is again call.
Same as this call.
And this is definition.
OK, so this this thing is optional.
So remember, I told you that if a function A wants to call A function B, then B must be defined above
A?
In this case, also this property is getting followed.
We are calling a function add and add.
A function is declared above mean function.
So this property holds true.
Okay.
So here I have written declared.
I have not done defined, okay?
I have not written defined.
I have written declared.
So this thing.
So this thing is actually definition the declaration both.
I am telling the compiler that I will write our ad function and simultaneously, simultaneously.
I have written ad function, so I have defined my function and I have declared my function.
But in this case, I told the compiler that I will be writing ad function.
And here below mean I remember I have written below mean and below mean.
I will write ad function.
So let's do some problem to get a better understanding.
Okay.
Function dard CBP.
Now, our first task is to Brent hello.
So what do we do?
I will create a function.
Brent, hello.
So I am creating a function.
Brent Hello.
So will it take something as input?
No, it will not take anything as input because it doesn't require anything as input.
Now, if you were on this program, it will show us at it.
Let's see.
And what is data, it is saying, I don't know where disparate tell.
So let's say let's declare Brint hello function.
So.
While Brent, hello.
It will not take anything as input and it will not return anything.
Okay.
So void means do not done anything.
So its work is to just print how loud do they use it?
So we'll see out.
Hello.
And that is all that we have to do.
OK.
Now, let us get in our program.
So health is getting branded.
OK, so how it works.
OK, so one more thing.
For example, if I were done here, see out.
World, so virtually my output.
So help will get printed first.
Or Voil will get printed first.
So Halo is getting brain dead first.
Then wireless printer.
So the flow is.
So remember my compilation compilation start from the first line, the end.
But the execution of the program will start from mean.
The execution of program start from mean.
So this is this was completion.
And this is execution of program.
So execution of program starts from Min Compiler will reach line number 10.
Brent, hello.
I will stop.
I will stop here.
Stop.
Compiler will come here.
Now it will execute function Rintel, so Hailo will get printed, Halo will get printed.
Now, after doing the work, it will come again at line number 10.
What gives or what?
Go to nine.
Number eleven, Brint World.
So the main thing that I want to tell you is, for example, if a function A has, let's say 10 lines
a function, it has 10 lines and headline number five at line number five.
It is calling A function B.
So this is my function B, so at line number five, function A is calling function B.
So what will happen?
The execution of function A execution of function.
Will stop.
Line number six will not be executed.
The execution of functioning will stop.
Compiler will reach here.
First of all, it will execute all the instructions of function B and after finishing B it will again
come to line number five.
Then line number six will be executed.
Then line number seven will be executed.
So what happened is it is called a Goyle stack.
Call stack.
So call stack or something like.
First of all, I am having only one function.
That does mean.
Maine is calling a function at line number 10.
Hello, so hello function will be called Brentano's Audy.
Brent, hello.
Function will be called.
OK.
Now, whatever that thing at the top will be executed.
So, Brent, hello.
Is that the top?
So Brent Taylor will be executed.
And when the Brent Taylor work is over, it will be Bob doubt it will be popped out from the stack.
And.
Men will be left here.
So whatever the thing at the top will be executed.
So now men will be executed.
And when the man is finished, it will be fobbed out.
And my stack will become empty.
OK.
Now, let us create a function.
This function where this function will do, this function will add two numbers.
OK.
So I want a function there to add two numbers.
And give me the final answer.
So the return type is end.
And then near the function is, let's say add.
And what are the two numbers?
Now do you have to add.
He will take input.
So Brent function was not taking anything as input because it doesn't require.
But add function.
Need to take two numbers as input.
So it will take two numbers as input.
Let's say the numbers are A and B..
So Ian, we are two numbers needed to take as input what it will do.
It will return the addition of these two numbers.
OK.
So it will return A plus B.
OK.
Now let us call function.
So, Ed, let's give two numbers to commentary, OK?
So first ladies in this forum.
So hello is getting printed and Allision is going on here, but there is nothing printed.
Why?
Because this ad function returns and indeed yeah that is five.
So this ad function returns five.
But you have not done anything with five.
So I think we get printed.
So this ad function is returning and digit.
And let's say I am storing that integer in a variable called C..
So what does it contain.
C contain five.
And now I will print.
See?
So what my output, my will be.
Hello.
And five.
So hello.
And faith is my output.
So how it works.
So this is my stack, this is my stack.
It is it is called caustic initially.
There is only mean deciding on the stack.
So I am executing mine.
I am executing mine.
Compiler at East Adlen, No.15, Brent, low function is called.
Brent, L.O. function is called now, Brent High-Low is at the top.
OK.
And it will print.
Hello.
So let's say, hey, is my output.
So Healthway got branded.
And then hello.
Get finished.
So hello is Bob Dowd.
So this is my next stage.
Now, Maine is at the top.
I will execute mine.
So my program reached line number eight in.
Now, I am calling a function Ed.
So this ad function will come here.
Add function.
OK.
Now our function is at the top.
I will execute and function.
So it is returning some of to integers.
Now, this ad function is over ad line number eleven.
So it will be fobbed out.
Now, this is mean.
So what I will do, I will execute Maine because Maine is at the top.
So at lane number, Dean C is restoring the addition of two numbers.
Then I am bringing C dander down zero.
Then Maine is over.
Maine is our pop out.
Now the stack becomes empty and our program is ordered.
OK,
now let us write one more function.
So this function will done.
MI squared off number.
So square number.
So that don't type of this function is going to be an integer because it will have done this great offer
number, which is going to be an integer.
So what it will take as input, it will take a value as input, only one value.
OK.
So it will take one way, Lou, as input.
So first, let us calculate the squared off a, which is a doozy.
And now I will.
You're done.
Answer.
OK, now let us call function squared.
So you can also directly light C out squared.
And let's say you want to find the squared of 10.
OK.
So instead of storing instead of storing them in danger, you can likely print that in danger.
OK.
Same thing.
So a hundred is me squared off.
Ten.
OK.
Now, this function square is taking one argument as input.
OK.
So this thing is called arguments or you can say barometer's arguments or let's say barometer's.
Now, what will happen if I not give anything as input?
What will happen?
Let's see.
So this square function need indigenous input, but I am not giving anything as input.
So let's see what will happen.
Obviously, at every bit there and what this data and it will be, you can read from damage here.
So too few arguments.
So few arguments means less argument.
So integer square and so in function needs an argument.
And but you are not giving any argument.
So it is too few arguments.
OK.
Now, one thing.
Have you observed if this main function is calling Brent Telo function?
So this printhead the function is defined above mean.
Similarly, main function was calling ad function.
So add function was defined above mean.
So what will happen if you write function below mean.
So.
I am writing the function below Min.
So the compiler is giving me edit and that it is very obvious.
I told you that the completion of the program starts from the top line till the end.
So Compiler will reach at line number 22 and you are calling a function squared compiler will say,
I don't know what is square because compiler have not compiled compiler.
I have not reached line number 27 yet.
OK, one more thing.
As soon as compiler finds an error, for example, this is the start point.
This is the end point.
So as soon as compiler finds an error, it will stop.
It will stop.
It will not compile all these lines.
Okay, so our compiler is getting stopped at line number 22.
So what you can do to work this thing?
I told you, you have to tell the compiler, Hey, Compiler.
I will be writing a function square, but I am not writing it now.
I will write it later.
So in square, I will take I in dangerous input.
So.
Hey, compiler.
I will be writing a function square, but I don't know what I have to write.
What definition I have to write.
So I will write it later.
So when I call this function.
Please don't give me the compiler.
I will say.
Okay.
Your choice.
If you want to write it later, that is completely your choice.
So now this is correct.
OK.
So the idea is we have to give a number as input, so 10.
Now it is working.
OK.
So this thing is called forward declaration.
Forword declaration.
OK.
So this thing, the code inside the code, inside the function is called definition.
And this is call calling or function.
So call.
So this is call calling of function.
Okay, so forword declaration is optional.
If you are writing the definition, if you are writing the function like this.
OK.
So a declaration is optional.
OK.
So this is it for this video.
Thank you.