Hello Viewer! How are you I hope you well? In the previous lesson, we learned Function in C language ( Part 1 ). in this lesson we'll learn Function in C language ( Part 2 )...
Why use Function?
We use the function so that we do not have to write the same code repeatedly. this gives us the advantages that we write a code once and we can call it as often as we want and save our time from it. suppose you have to get the area of a triangle. and later you have to get to the area of other triangles in the same program. So in such a situation, we would not want to write the same code repeatedly. we would like to jump to Section of code that calculates the area. This Section Of Code is nothing but a Function.
With the help of the function, we can write the program more effectively.
Passing Value Between Function
Just by making the function our problem is not solvable, we have to call it to use that function. Understand with Following lines...
As the motorcycle gets worse then he fixes the mechanic. The mechanic himself does not fix, for that, we have to call him.
The mechanism used to convey information to the function is the "Argument". you have unknowingly used the arguments in the printf ( ) and scanf ( ) functions; the format string and the list of variables used inside the parenthesis in these functions are arguments. The arguments are sometimes called "Parameters".
Understand the following program.
#include
int calsum ( int x,int y, int z ) ;
int main()
{
int a,b,c,sum;
printf("Enter Any three Numbers");
scanf("%d%d%d",&a,&b,&c);
sum=calsum(a,b,c);
printf("Sum=%d\n",sum);
return 0;
}
int calsum(int x,int y,int z)
{
int d;
d=x+y+z;
return(d);
}
In this program, in main ( ) we receive the values of a,b and c through the keyboard and then output the sum of a,b and c. however, the calculation of sum is done in a different function called calsum ( ) if sum is to be calculated in calsum ( ) and value of a,b and c are received in main ( ), then we must pass on these values to calsum ( ), and once calsum ( ) calculates the sum, we must return it from calsum ( ) back to main ( ).
Output of this program is below...
Important Point Related to Above Program :
1) In this program from the function main ( ), the values of a,b and c are passed on to the function calsum ( ) and mentioning a, b and c in the parenthesis:
sum=calsum(a,b,c);
In this calsum ( ) function these values get collected in three variables x, y, and z:
int calsum(int x,int y,int z)
2) The variables a,b and c are called "actual arguments", whereas the variables x,y, and z are called "Formal Arguments". Any number of arguments can be passed to a function being called. however, the type, order, and number of the actual and formal arguments must always be same.
3) Note the function prototype declaration of calsum ( ). instead of the usual void we are using int. this indicates that the calsum ( ) is going to return a value of the type int. it is not compulsory to use variable names in the prototype declaration. Hence we could as well have written in prototype as:
int calsum ( int , int , int ) ;
In the definition of calsum ( ) too, void has been replaced by int.
In this part of Function in C language Part 2 so enough 4 today. I hope you like it. we'll discuss Scope Rule of Function in the future post.
Why use Function?
We use the function so that we do not have to write the same code repeatedly. this gives us the advantages that we write a code once and we can call it as often as we want and save our time from it. suppose you have to get the area of a triangle. and later you have to get to the area of other triangles in the same program. So in such a situation, we would not want to write the same code repeatedly. we would like to jump to Section of code that calculates the area. This Section Of Code is nothing but a Function.
With the help of the function, we can write the program more effectively.
Passing Value Between Function
Just by making the function our problem is not solvable, we have to call it to use that function. Understand with Following lines...
As the motorcycle gets worse then he fixes the mechanic. The mechanic himself does not fix, for that, we have to call him.
The mechanism used to convey information to the function is the "Argument". you have unknowingly used the arguments in the printf ( ) and scanf ( ) functions; the format string and the list of variables used inside the parenthesis in these functions are arguments. The arguments are sometimes called "Parameters".
Understand the following program.
#include
int calsum ( int x,int y, int z ) ;
int main()
{
int a,b,c,sum;
printf("Enter Any three Numbers");
scanf("%d%d%d",&a,&b,&c);
sum=calsum(a,b,c);
printf("Sum=%d\n",sum);
return 0;
}
int calsum(int x,int y,int z)
{
int d;
d=x+y+z;
return(d);
}
In this program, in main ( ) we receive the values of a,b and c through the keyboard and then output the sum of a,b and c. however, the calculation of sum is done in a different function called calsum ( ) if sum is to be calculated in calsum ( ) and value of a,b and c are received in main ( ), then we must pass on these values to calsum ( ), and once calsum ( ) calculates the sum, we must return it from calsum ( ) back to main ( ).
Output of this program is below...
Important Point Related to Above Program :
1) In this program from the function main ( ), the values of a,b and c are passed on to the function calsum ( ) and mentioning a, b and c in the parenthesis:
sum=calsum(a,b,c);
In this calsum ( ) function these values get collected in three variables x, y, and z:
int calsum(int x,int y,int z)
2) The variables a,b and c are called "actual arguments", whereas the variables x,y, and z are called "Formal Arguments". Any number of arguments can be passed to a function being called. however, the type, order, and number of the actual and formal arguments must always be same.
3) Note the function prototype declaration of calsum ( ). instead of the usual void we are using int. this indicates that the calsum ( ) is going to return a value of the type int. it is not compulsory to use variable names in the prototype declaration. Hence we could as well have written in prototype as:
int calsum ( int , int , int ) ;
In the definition of calsum ( ) too, void has been replaced by int.
In this part of Function in C language Part 2 so enough 4 today. I hope you like it. we'll discuss Scope Rule of Function in the future post.
Thank You 4 reading my blog! if you have any problem or issue with this post please comment/contact.
Important :
In this post, Article/Paragraph is taken from Let Us C book that is written by Yashavant Kanetkar.