C Function

Advertisements

Function in C

A function is a block of codes that performs a specific task and may return value. The main() function is the first user defined function invoked by the compiler. While it is possible to write any code within main function, it leads number of problems. The program may become too large and complex and it is difficult to test, debugg and maintain the complex code. For that reason, We use function to place independent code in separate modules called function or subprogram. In order to make a program using function, we need to perform the followling three steps.

  • Function declaration
  • Function definition
  • Function call

Function declaration

Like variables, all the functions must be declared. Function declaration statement includes function name, what function will take and what function will return.

Syntax :


              return-type function-name(argument list);

return-type : type of value function will return.

function-name : any valid C identifier.

argument list : represents the type and number of value function will take, values are sent by the calling statement.

Example for declaration of function

If we want to return the sum of two integer numbers and function will take two numbers as argument then the function declaration statement will be:


              int Add(int, int);

Function definition

Function definition includes the actual working or implementation.

Syntax for defining function


              return-type function-name(argument list)
              {
                           - - - - - - - - - -
                           body of function
                           - - - - - - - - - -
              }

The body of function contains the number of statements to perform specific task.

Example for definition of function

The body of function for calculating sum of two integer numbers.


              int Add(int x,int y)
              {
                     int sum;

                      sum = x + y;

                     return sum;
              }

Function call or Function invoke

To execute function we must call it. A function can be called or invoked by using function name followed by list of arguments (values) that function definition will recieve to perform task.

Syntax for calling or invoke function


              var = function-name(val1,val2...n);
  			

var can be any variable that will recieve value returning from function definition.

Example for calling or invoke function

Considering the above example, function calling statement should be :


              int rs;
              rs = Add(10,20);        //calling statement
              printf("\nThe sum is : %d",rs);

Passing argument to a function

Like normal variable, pointer variable can be passed as function argument and it can return from function.

There are two approaches to passing argument to a function:

  • Call by Value
  • Call by Reference/Address

Call by Value

In this approach, the values are passed as function argument to the definition of function.

Example of call by value


	#include<stdio.h>

       void main()
       {
              int A=10,B=20;

              printf("\nValues before calling %d, %d",A,B);

              fun(A,B);                           //Statement     1

              printf("\nValues after  calling %d, %d",A,B);

       }

       void fun(int X,int Y)                   //Statement     2
       {
              X=11;
              Y=22;
       }

   Output :

              Values before calling 10, 20
              Values after  calling 10, 20

				

In the above example, statement 1 is passing the values of A and B to the calling function fun(). fun() will recieve the value of A and B and put it into X and Y respectively. X and Y are value type variables and are local to fun(). Any changes made by value type variables X and Y will not effect the values of A and B.


		  
				

Call by Reference

In this approach, the references/addresses are passed as function argument to the definition of function.

Example of call by reference


	#include<stdio.h>

       void main()
       {
              int A=10,B=20;

              printf("\nValues before calling %d, %d",A,B);

              fun(&A,&B);                           //Statement     1

              printf("\nValues after  calling %d, %d",A,B);

       }

       void fun(int *X,int *Y)                   //Statement     2
       {
              *X=11;
              *Y=22;
       }

   Output :

              Values before calling 10, 20
              Values after  calling 11, 22

				

In the above example, statement 1 is passing the reference of A and B to the calling function fun(). fun() must have pointer formal arguments to recieve the reference of A and B. In statement 2 *X and *Y is recieving the reference A and B. *X and *Y are reference type variables and are local to fun(). Any changes made by reference type variables *X and *Y will change the values of A and B respectively.


  
		

Difference between Call by Value and Call by Reference.

Call by ValueCall by Reference
The actual arguments can be variable or constant.The actual arguments can only be variable.
The values of actual argument are sent to formal argument which are normal variables.The reference of actual argument are sent to formal argument which are pointer variables.
Any changes made by formal arguments will not reflect to actual arguments.Any changes made by formal arguments will reflect to actual arguments.

Related topics

Advertisement