In the previous lesson, we have learned functions in C. So, in this lesson, we will learn about function call in C and the types of function calls in C.
Table of Contents
What is Function Call in C?
C language program does not execute the statements in a function definition until the function is called. When a program calls a function. the program control is transferred
to the called function.
A called function performs a defined task and when its return statement is executed or when its function-ending closing brace is reached, it returns the program control back
to the main( ) or calling function.
To call a function, you simply need to pass the required parameters along with the function name, and if the function returns a value, then you can store the returned value in a variable.
//Learnprogramo #include<stdio.h> int sum(); void main() { int result; printf("\nGoing to calculate the sum of two numbers:"); result = sum(); printf("%d",result); } int sum() { int a,b; printf("\nEnter two numbers"); scanf("%d %d",&a,&b); return a+b; }
A function can be called more than once, so the code is, executed each time it is called. To understand how C performs function calls, we first need to consider a data structure (i.e., collection of related data items) known as a stack.
Introduction to Function Call in C:
Stacks are known as last-in, first-out (LIFO) data structures-the last item pushed (inserted) on the stack is the first item popped (removed) from the stack.
As each function is called, it may call other functions, which may call other functions – all before any function returns. Each function eventually must return control to the function that called it.
So, we must keep track of the return addresses that each function needs to return control to the function that called it. The function call in c stack is the perfect data structure for handling this information.
Each time a function calls another function, an entry is pushed onto the stack. This entry, called a stack frame, contains the return address that the called function needs in order to return to the calling function. It also contains some additional information like local variables.
If the called function returns, instead of calling another function before returning, the stack frame for the function call in c is popped, and control transfers to the return address in the popped stack frame.
Parameter Passing (By Value)
Function parameters or arguments are the means of communication between the calling and the called functions. The function i.e. main( ) and user-defined function interacts with each other through parameters.
The parameter may classify under two groups namely, Formal Parameters and Actual Parameters.
Formal or Dummy Parameters:
The formal parameters are the parameters given in the function declaration and function definition. When the function is invoked, the formal parameters are replaced by the actual parameters.
Actual Parameters:
The parameters appearing in the function call are referred to as actual parameters. The actual arguments may be expressed as constants, single variables or more complex expression.
The actual parameters must correspond to the formal parameters in the function definition; i.e., the number of actual parameters must be the same as the number of formal parameters, and each actual parameter must be of the same data type as its corresponding formal parameter.
Program:
//Learnprogramo #include<stdio.h> int main( ) { int x,y; int add(int a, int b); printf("enter the value for x,y"); scanf("%d%d",&x,&y); printf("sum is=%d",add(x,y)); return 0; } int add(int a, int b) { int c; c=a+b; return(c); }
The variables a and b defined in the function definition are known as formal parameters. The variables x and y are actual parameters.
Difference Between Formal and Actual Parameters:
Formal Parameter | Actual Parameter |
---|---|
Formal Parameters are used in the function header and function definition. | Actual Parameters are used in the function call in c. |
It is used to receive the values that are passed to the function through a function call. | They are the actual values that are sent to the function definition through the function call. |
Formal Parameters are treated as local variables of the function in which they are used in the function header. | Actual Parameters may be constant values or variables names(local or global). |
In formal parameters, the data types of the receiving values should be included. | In actual parameters. there is no mentioning of data types. Only the value is mentioned. |
Methods for Parameter Passing to Function Call in C
Parameter passing is basically to have communication between called function and calling function. There are two methods of parameter passing namely, Call by value and Call by reference.
1. Call by Value:
In call by value, during the function call, the actual parameter value is copied and passed to the formal parameter. Changes made to the formal parameters do not affect the actual parameters.
When a single value is passed to a function via an actual argument, the value of the actual argument is copied into the function.
Therefore, the value of the corresponding formal argument can be altered within the function, but the value of the actual argument within the calling function will not change. This procedure for passing the value of a parameter to a function is known as passing by value.
Thus in a call by value, a copy of actual arguments is passed to formal arguments of the called function and any change made to the formal arguments in the called function have no effect on the values of actual arguments in the calling function.
A value of the actual parameter cannot be modified by the formal parameter. Different Memory is allocated for both actual and formal parameters.
//Learnprogramo #include <stdio.h> int increment(int var) { var = var+1; return var; } int main() { int num1=20; int num2 = increment(num1); printf("num1 value is: %d", num1); printf("\nnum2 value is: %d", num2); return 0; }
Output:
2. Call by Reference:
In call by reference, the address of actual arguments is passed to formal arguments of the called function. This means any changes made in formal parameters in called function is reflected into actual parameters of calling function.
Thus, the value of the actual parameter can be modified by the formal parameter. Same memory is used for both actual and formal parameters since the only address is used by both parameters.
It uses pointers to pass the reference of an actual parameter to formal parameter.
//Learnprogramo void swapnum ( int *var1, int *var2 ) { int tempnum ; tempnum = *var1 ; *var1 = *var2 ; *var2 = tempnum ; } int main( ) { int num1 = 25, num2 = 50 ; printf("Before swapping:"); printf("\nnum1 value is %d", num1); printf("\nnum2 value is %d", num2); swapnum( &num1, &num2 ); printf("\nAfter swapping:"); printf("\nnum1 value is %d", num1); printf("\nnum2 value is %d", num2); return 0; }
Output:
Difference Between Call by Value and Call by Reference
Call by Value | Call by Reference |
---|---|
This method copy original value into function as a arguments. | This method copy address of arguments into function as a arguments. |
Changes made to the formal parameter inside the function have no effect on the actual argument. | Changes made to the formal parameter affect the actual argument. Because address is used to access the actual argument. |
Actual and formal arguments will be created in different memory location. | Actual and formal arguments will be created in the same memory location. |
It does not require pointer concept. | It requires pointer concept. |
Passing by value is restricted to a one-way transfer of information. | Passing by reference is the two-way transfer of information. |
Return Statement
The return statement is used to terminate the execution Of a function and transfer program control back to the calling function. In addition, it can specify a value to be returned by the function.
A function may contain one or more return statements. The general format/syntax of the return the statement is given below:
return; OR return expr;
Where expr is the expression whose value is returned by the function. It is optional to enclose the returning value in parentheses.
The execution of the return statement causes the expression expr to be evaluated and its value to be returned to the point from which this function is called.
Only one expression can be included in the return statement. Thus, a function can return only one value to the calling portion Of the program via return.
The expression expr in the return statement is optional and if omitted, program control is transferred back to the calling function without returning any value.
We can use multiple return statements in a function but as soon as the first return statement is encountered the function terminates and all the statements following it are not executed.
Example:
//Learnprogramo #include <stdio.h> int SUM(int a, int b) { int s1 = a + b; return s1; } int main() { int num1 = 10; int num2 = 10; int sum_of = SUM(num1, num2); printf("The sum is %d", sum_of); return 0; }
Output:
Conclusion:
In this lesson, we have learned function call in C. Now, in the next lesson, we will learn Recursion using functions.
Also Read: