Friday, July 29, 2016

C Basics - Calling a Function in C

Calling a 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.

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 program.

#include <stdio.h>
 
/* function declaration */
int sum(int num1, int num2);
 
int main () {

   int i = 0;
   int j = 0;
   int ret;
 
   printf ( "\nEnter the first number: " ) ; 
   scanf ( "%d", &i ) ;
   printf ( "\nEnter the second number: " ) ; 
   scanf ( "%d", &j ) ;

   /* calling a function to get sum value */
   ret = sum(i, j);
 
   printf( "Sum value is : %d\n", ret );
 
   return 0;
}
 
/* function returning sum of two numbers */
int sum(int num1, int num2) {

   int result;
 
   result = num1 + num2;
 
   return result; 
}
Output of above program,

Enter the first number: 4

Enter the second number: 5
Sum value is : 9

Function Arguments:
The names given in function definition are called parameter. Parameter written in function definition is called formal parameter. Parameter written in function call is called actual parameter. The values supplied in the function call are called arguments.

While calling a function, there are two ways in which arguments can be passed to a function.
Call by Value: This method copies the actual value of an argument into the formal parameter of the function. In this case, changes made to the parameter inside the function have no effect on the argument.

Call by reference: This method copies the address of an argument into the formal parameter. Inside the function, the address is used to access the actual argument used in the call. This means that changes made to the parameter affect the argument.

By default, C uses call by value to pass arguments. In general, it means the code within a function cannot alter the arguments used to call the function.

Call by Value:
The call by value method of passing arguments to a function copies the actual value of an argument into the formal parameter of the function. In this case, changes made to the parameter inside the function have no effect on the argument.

#include <stdio.h>
 
/* function declaration */
int swap(int x, int y);
 
int main () {

   int i = 10;
   int j = 20;
   
   printf("Before swap, value of i : %d\n", i );
   printf("Before swap, value of j : %d\n", j );

   /* calling a function to swap values of i and j */
   swap(i, j);
 
   printf("After swap, value of i : %d\n", i );
   printf("After swap, value of j : %d\n", j );
 
   return 0;
}
 
/* function to swap two values */
int swap(int x, int y) {

   int temp;
 
   temp = x;
   x = y;
   y = temp;
 
   return 0; 
}
Output of above program,

Before swap, value of i : 10
Before swap, value of j : 20
After swap, value of i : 10
After swap, value of j : 20
It shows that there are no changes in the values of i and j, though they had been changed inside the function.

Call by reference:
The call by reference method of passing arguments to a function copies the address of an argument into the formal parameter. Inside the function, the address is used to access the actual argument used in the call. It means the changes made to the parameter affect the passed argument.
To pass a value by reference, argument pointers are passed to the functions just like any other value.

#include <stdio.h>
 
/* function declaration */
int swap(int *x, int *y);
 
int main () {

   int i = 10;
   int j = 20;
   
   printf("Before swap, value of i : %d\n", i );
   printf("Before swap, value of j : %d\n", j );

   /* calling a function to swap values of i and j */
   swap(&i, &j);
 
   printf("After swap, value of i : %d\n", i );
   printf("After swap, value of j : %d\n", j );
 
   return 0;
}
 
/* function to swap two values */
int swap(int *x, int *y) {

   int temp;
 
   temp = *x;
   *x = *y;
   *y = temp;
 
   return 0; 
}
Output of above program,

Before swap, value of i : 10
Before swap, value of j : 20
After swap, value of i : 20
After swap, value of j : 10
It shows that the change has reflected outside the function as well, unlike call by value where the changes do not reflect outside the function.



Related topics:
Functions in C   |   Function Definition and Declaration in C   |   Recursion in C   |   Variable Arguments in C   |   Command Line Arguments in C

List of topics: C Programming

No comments:

Post a Comment