Hello Viewer! How are you I hope you well? In the previous lesson, we learned Function in C language ( Part 2 ). in this lesson we'll learn Function in C language ( Part 3 )...
Scope Rule of Function
understand the following program...
#include< stdio . h >
void display(int);
int main()
{
int i=20;
display(i);
return 0;
}
void display(int j)
{
int k=5;
printf("%d\n",j);
printf("%d\n",k);
}
In this program, can you tell us why we have passed the variable i in the display( ) function?will it not become automatically available to the function display ( )? No because, by default, the scope of variable is local to the function in which it is defined. The presense of i is known only to the function main ( ) and not to any other function. Similarly, the variable k is the local for the function display ( ) we have to explicitly pass it to display( ). Likewise, if we want k to be available to main ( ) using the return statement. In general, we can say that the scope of a variable is local to the function in which it is defined.
Order of Passing Arguments
See the following function call:
fun ( a,b,c ) ;
In this function call doesn't matter whether the argument passed from left 2 right or from right 2 left. whereas in some funtion call it matter to what value passed in argument. Understand with following example...
int a = 1 ;
printf ( "%d%d%d\n", a,++a, a++ ) ;
Output: 122
This is, however, is not the case. Surprisingly, it outputs 3 3 1. This is because, in C during a function Call the arguments are passed from right 2 left. That is 1stly 1 is passed through the expression a++ and then a is incremented to 2. Then the result of ++a passed. That is a, is incremented to 3 and then passed. Finally, the latest value of a, i.e., 3, is passed. Thus in right to left order, 1 , 3 , 3 get passed. Once printf ( ) collects them, it prints them in the order in which we asked it to get them printed. Thus 3 3 1 gets printed.
Using Library Functions
Understand the following program:
#include
#include
int main()
{
float a=0.5;
float w,x,y,z;
w= sin (a);
x= cos (a);
y= tan (a);
z= pow(a,2);
printf("%f\n%f\n%f\n%f",w,x,y,z);
return 0;
}
Scope Rule of Function
understand the following program...
#include< stdio . h >
void display(int);
int main()
{
int i=20;
display(i);
return 0;
}
void display(int j)
{
int k=5;
printf("%d\n",j);
printf("%d\n",k);
}
In this program, can you tell us why we have passed the variable i in the display( ) function?will it not become automatically available to the function display ( )? No because, by default, the scope of variable is local to the function in which it is defined. The presense of i is known only to the function main ( ) and not to any other function. Similarly, the variable k is the local for the function display ( ) we have to explicitly pass it to display( ). Likewise, if we want k to be available to main ( ) using the return statement. In general, we can say that the scope of a variable is local to the function in which it is defined.
Order of Passing Arguments
See the following function call:
fun ( a,b,c ) ;
In this function call doesn't matter whether the argument passed from left 2 right or from right 2 left. whereas in some funtion call it matter to what value passed in argument. Understand with following example...
int a = 1 ;
printf ( "%d%d%d\n", a,++a, a++ ) ;
Output: 122
This is, however, is not the case. Surprisingly, it outputs 3 3 1. This is because, in C during a function Call the arguments are passed from right 2 left. That is 1stly 1 is passed through the expression a++ and then a is incremented to 2. Then the result of ++a passed. That is a, is incremented to 3 and then passed. Finally, the latest value of a, i.e., 3, is passed. Thus in right to left order, 1 , 3 , 3 get passed. Once printf ( ) collects them, it prints them in the order in which we asked it to get them printed. Thus 3 3 1 gets printed.
Using Library Functions
Understand the following program:
#include
#include
int main()
{
float a=0.5;
float w,x,y,z;
w= sin (a);
x= cos (a);
y= tan (a);
z= pow(a,2);
printf("%f\n%f\n%f\n%f",w,x,y,z);
return 0;
}
Here we have called the statdard library functions - sin ( ), cos ( ), tan ( ), pow ( ). We know that, before calling any function, we must declare its prototype. This helps the compiler in checking whether the values being passed and returned are as per the prototype declaration. But since we didn't define the library function, we do not know the prototype declarations of library functions. Hence, when the library function is provided, a set of ".h ( dot h ) " files is also provided. These files contain the prototype declaration of library functions.But why multiple files? Because the library function are divided into different group. For example , prototypes of all input/output functions are provided in the "stdio . h ", prototypes of all mathematical functions are provided in the library file " math . h ", etc.
Prototypes of functions sin ( ), cos ( ), tan ( ), pow ( ) are declared in the file " math . h ". To make these prototypes available to our program we need to #include the file " math . h ". You can even open this file and look at the prototypes. They would appear as below...
double sin ( double ) ;
double cos ( double ) ;
double tan ( double ) ;
double pow ( double, double ) ;
Here double indicates a real number.
One Dicey Issue
See the following program ...
#include
int main()
{
int i=10, j=20 ;
printf ("%d%d%d\n",i,j);
printf ("%d\n",i,j);
return 0;
}
This program gets successfully compiled, even though there is a mismatch in the format specifiers and the variable in the list is used in printf ( ) accepts a variable number of arguments and even with mismatch above, the call still matches with the prototype of printf ( ) present in " stdio . h ". At run-time, when the first printf ( ) is executed since there is no variable matching with the last specifier %d, a garbage integer gets printed. Similarly, in the second printf ( ), since the format specifier for j has not be mentioned, its value does not get printed.
Return Type of Function
Suppose we want 2 find square of a floating point number using a function. This is how this program show below...
#include
float Square(float);
int main()
{
float a,b;
printf ("Enter Any Number");
scanf ("%f",&a);
b= Square(a);
printf("Square of %f is %f\n",a,b);
return 0;
}
float Square(float x)
{
float y;
y=x*x;
return(y);
}
Output : See the picture...
Since we are returning a float value from this function we have indicate the return type of the Square ( ) function as float in the prototype declaration as well as in the function definition. Had we dropped float from the prototype and the definition, the compiler would have assumed that Square ( ) is supposed to return as integer value.
Summary
1) Function declaration specifies the return type of the function and the types of parameters it accepts.
2) Function definition defines the body of the function.
3) In C order of passing arguments to a function is from right 2 left.
In this part we complete the Function in C. I hope you like it. we'll discuss Next Topic 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.