Hey Everyone, I am Vikram Salunke and welcome
to x86 Assembly course.
Now let's look at the Functions.
Function is a group of statements that together
perform a task.
Every C program has at least one function
main().
Function declaration tells the compiler about
function's name, it's return type and it's
parameters.
Function definition provides actual body of
function.
Now let's see function in a program.
This is my C program.
First we are including stdio.h file to include
all the functions from stdio.h header file.
After that we directly go to the main().
Always start reading your program from the
main().
Here, we are defining 3 variables num1, num2
and total.
After that we are printing on a screen i.e.
Enter the num1 then user will enter number1
and it will be stored in a num1.
Which we have declared here.
After that we are printing on a screen Enter
number 2 and we are saving the value entered
by the user into the num2.
After that we are using total = add(num1,
num2).
So, the total is 3rd variable which is saving
the total of a number1 and number2.
Let's look at the what is this add.
In the upper section we can see a function
called int add(int num1, int num2).
We are defining a function here known as a
add.
It's data type is int it has a 2 parameters
int num1, int num2. main() is passing the
num1 and num2 to the add function.
We have to define it's parameter here.
The both parameters of a data type int. { means
this function's body will start here.
This function is adding num1 and num2 and
returning.
So, where it's returning? it's returning to
the total.
The total variable will now get addition of
number1 and number2 and what we are doing?
we are printing the total onto the screen.
And we are exiting the program by placing
the return value 0.
So, now we will run this program.
First we will put the breakpoint here.
After that we will build the program.
Program has been successfully built.
Now we will run the program by clicking start
debugging.
The program is asking for number1 let's enter
10 number2 let's enter 20 that means we are
getting the total of 2 numbers.
Now let's see this program in slides once
again and let's understand line by line.
First line is as usual #include which
is telling the compiler to include the stdio.h
file before going to actual compilation.
After that int main(){ that means main() function
where program execution will begin and { will
tell the compiler that main program's body
has started.
After that we are defining 3 integer variables.
We are asking for number 1 using printf command.
So, using the scanf function we are saving
the input entered by the user into num1 variable.
And we have used %d because num1 is integer.
The next line is printf("Enter num2"); so,
we are asking for number2 after that we are
getting the input from the user and storing
it in a num2 variable %d because num2 is int.
Now, we are calling function add, we are passing
execution to add() and we are passing num1
and num2 as an argument to function add().
We are storing the return value into the variable
total.
The next line is int add(int num1, int num2){
that means we are declaring the function add()
and we are declaring num1 and num2 as that
function's parameter.
And we are using int add that means the add
function will return integer value.
So, the next line is return num1 + num2; } we
are adding num1 + num2 and returning the result
of an addition to the main() function.
So, we are printing the total on a screen.
The next line is return 0.
That means we are terminating the main() function
and returning the value 0.
And the main() program's body has been ended
here.
So that's what I had planned for this video.
If you have any doubts, any questions you
can mail me on vikram@vmaskers.com or you
can tweet me @vikramsalunke20.
Thank you :)
