Session – 19
Function pointer, call by value &
call by reference
Session Outcomes
• Function pointer or pointer to function
– Stores the address of a function
• Different methods of calling a function
– Call By Value
– Call By Reference
CALLING FUNCTION IN MAIN
void sum()
{
int a=2,b=3;
printf(“ answer =%d”, a+b);
}
int main()
{
void(*ptr) (void); // declaring function pointer
ptr=sum; // pointer storing address of function
ptr(); //calling of function written before main
}
Wednesday, June 10, 2020
For suggestions or queries contact
drkrk@kluniversity.in
STEPS TO DECLARE FUNCTION POINTER
• Declare the function pointer-
– Function pointer is declared by any name with precedence of *
operator.
– This pointer is written into opening closing rounded bracket.
– Return data type if any will be written first then pointer with
rounded brackets then data type of actual parameters will be
written in rounded brackets.
• Declaring function pointer does not means that function can be
directly called.
• Store the address- to call the function address of function must be
stored in function pointer
• Call the function using function pointer- instead of using function
name to call it now use function pointer name to call the function (if
arguments are there then pass it i.e. actual parameters)
Declaring a Function Pointer
Normal function:
void sum(int,int);
Its Function pointer:
void (*ptr)(int,int)
Wednesday, June 10, 2020
For suggestions or queries contact
drkrk@kluniversity.in
Return
type
Name of the
function
Argument (Actual
Parameters)
Explanation
void add (int , int ); Add is such a function which
accepts two integers and prints
addition of them
void (*ptr) (int, int); ptr is a pointer to such function
which accepts two integers.( the
function can do any thing. In this
case it will do addition)
Call by value
• Copies the value of actual parameters
into formal parameters.
• During execution whatever changes are
made in the formal parameters are not
reflected back in the actual parameters.
Call by Reference
• Reference(address) of the original variable is passed.
• Function does not create its own copy, it refers to the
original values by reference.
• Functions works with the original data and changes
are made in the original data.
Wednesday, June 10, 2020
For suggestions or queries contact
drkrk@kluniversity.in
Call by Reference
• Sometimes we need a function to return multiple
values.
• However, functions can only have one return value.
• One way to return multiple values is using reference
parameters.

Function Pointer in C

  • 1.
    Session – 19 Functionpointer, call by value & call by reference Session Outcomes • Function pointer or pointer to function – Stores the address of a function • Different methods of calling a function – Call By Value – Call By Reference
  • 2.
    CALLING FUNCTION INMAIN void sum() { int a=2,b=3; printf(“ answer =%d”, a+b); } int main() { void(*ptr) (void); // declaring function pointer ptr=sum; // pointer storing address of function ptr(); //calling of function written before main } Wednesday, June 10, 2020 For suggestions or queries contact drkrk@kluniversity.in
  • 3.
    STEPS TO DECLAREFUNCTION POINTER • Declare the function pointer- – Function pointer is declared by any name with precedence of * operator. – This pointer is written into opening closing rounded bracket. – Return data type if any will be written first then pointer with rounded brackets then data type of actual parameters will be written in rounded brackets. • Declaring function pointer does not means that function can be directly called. • Store the address- to call the function address of function must be stored in function pointer • Call the function using function pointer- instead of using function name to call it now use function pointer name to call the function (if arguments are there then pass it i.e. actual parameters)
  • 4.
    Declaring a FunctionPointer Normal function: void sum(int,int); Its Function pointer: void (*ptr)(int,int) Wednesday, June 10, 2020 For suggestions or queries contact drkrk@kluniversity.in Return type Name of the function Argument (Actual Parameters) Explanation void add (int , int ); Add is such a function which accepts two integers and prints addition of them void (*ptr) (int, int); ptr is a pointer to such function which accepts two integers.( the function can do any thing. In this case it will do addition)
  • 5.
    Call by value •Copies the value of actual parameters into formal parameters. • During execution whatever changes are made in the formal parameters are not reflected back in the actual parameters.
  • 7.
    Call by Reference •Reference(address) of the original variable is passed. • Function does not create its own copy, it refers to the original values by reference. • Functions works with the original data and changes are made in the original data.
  • 9.
    Wednesday, June 10,2020 For suggestions or queries contact drkrk@kluniversity.in Call by Reference • Sometimes we need a function to return multiple values. • However, functions can only have one return value. • One way to return multiple values is using reference parameters.