C Programming Tutorial

Advertisements

Function Pointer in C

Like normal variable, pointer variable can be passed as function argument and function can return pointeras well.

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


       #include<stdio.h>

       void fun(int,int);

       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:


       #include<stdio.h>

       void fun(int*,int*);

       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 b/w 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.

Function Returning a Pointer

Like normal variable, a function can also return reference or address. When function returns reference or address, the return type of function must be pointer type.

Syntax for function returning pointer:


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

Example for function returning pointer:


       #include<stdio.h>

       int *reference(int);

       void main()
       {
              int A=10;
              int *ptr;

              printf("\nAddress of %d in main() is %u",A,&A);
              ptr = reference(A);
              printf("\nAddress of %d in reference() was %u",A,ptr);
       }

       int *reference(int n)
       {
              return &n;
       }

   Output :

              Address of 10 in main() is 8736
              Address of 10 in reference() was 8730

Pointer to Function

Like normal variable, Every function has reference or address, and if we know the reference or address of function, we can access the function using its reference or address. This is the another way of accessing function using pointer.

Syntax for declaring a Pointer to function:


              return-type (*ptr-function)(argument list);

return-type : type of value function will return.

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

(*ptr-function) : The parentheses around *ptr-function tells the compiler that it is pointer to function.

If we write *ptr-function without parentheses then it tells the compiler that ptr-function is a function that will return a pointer.

The pointer to function will be accessed as:


              (*ptr-function)(val1,val2...n);

Example : Sum of two numbers using pointer to function.


       #include<stdio.h>

       int Sum(int,int);
       int (*ptr)(int,int);

       void main()
       {
              int a,b,rs;

              printf("\nEnter 1st number : ");
              scanf("%d",&a);

              printf("\nEnter 2nd number : ");
              scanf("%d",&b);

              ptr = Sum;                 //Statement   1
              rs = (*ptr)(a,b);          //Statement   2

              printf("\nThe sum is : %d",rs);
       }

       int Sum(int x,int y)
       {
              return x + y;
       }

   Output :

              Enter 1st number : 78
              Enter 2nd number : 45
              The sum is : 123

In the above program, Statement 1 is assigning the address of Sum() to pointer ptr. Statement 2 is calling and passing two values to Sum().

Related topics

Advertisement