Function Syntax of function Declaration section <<Returntype>> funname(parameter list); Definition section <<Returntype>> funname(parameter list) { body of the function } Function Call Funname(parameter);
Example #include<stdio.h> void fun(int a); //declaration int main() { fun(10); //Call } void fun(int x) //definition { printf(“%d”,x); }
ACTUAL & FORMAL PARAMETERS Actual parameters are those that are used during a function call Formal parameters are those that are used in function definition and function declaration
Call by value Calling a function with parameters passed as values int a=10; void fun(int a) fun(a); { defn; } Here fun(a) is a call by value. Any modification done within the function is local to it and will not be effected outside the function
Call By Reference Calling a function by passing pointers as parameters (address of variables is passed instead of variables) int a=1; void fun(int *x) fun(&a); { defn; } Any modification done to variable a will effect outside the function also
Illustration a and x are referring to same location. So value will be over written.
Illustration
Difference between call by value and call by reference Call by value => copying value of variable in another variable. So any change made in the copy will not affect the original location Call by reference => Creating link for the parameter to the original location. Since the address is same, changes to the parameter will refer to original location and the value will be over written

Pre defined Functions in C

  • 1.
    Function Syntax of function Declarationsection <<Returntype>> funname(parameter list); Definition section <<Returntype>> funname(parameter list) { body of the function } Function Call Funname(parameter);
  • 2.
    Example #include<stdio.h> void fun(int a);//declaration int main() { fun(10); //Call } void fun(int x) //definition { printf(“%d”,x); }
  • 3.
    ACTUAL & FORMALPARAMETERS Actual parameters are those that are used during a function call Formal parameters are those that are used in function definition and function declaration
  • 4.
    Call by value Callinga function with parameters passed as values int a=10; void fun(int a) fun(a); { defn; } Here fun(a) is a call by value. Any modification done within the function is local to it and will not be effected outside the function
  • 5.
    Call By Reference Callinga function by passing pointers as parameters (address of variables is passed instead of variables) int a=1; void fun(int *x) fun(&a); { defn; } Any modification done to variable a will effect outside the function also
  • 6.
    Illustration a and xare referring to same location. So value will be over written.
  • 7.
  • 8.
    Difference between callby value and call by reference Call by value => copying value of variable in another variable. So any change made in the copy will not affect the original location Call by reference => Creating link for the parameter to the original location. Since the address is same, changes to the parameter will refer to original location and the value will be over written