PARAMETER PASSING TECHNIQUES - C-Tutorial

Latest

Monday, 3 October 2016

PARAMETER PASSING TECHNIQUES

PARAMETER PASSING TECHNIQUES (or)  
TYPES OF FUNCTION CALLS (or)
 PASSING ARGUMENTS TO FUNCTIONS


            Arguments can be passed to a function in two ways. Those are,
1.                  Call by value (or) Pass by value
2.                  Call by address (or) Pass by address

1. Call by value:         The process of passing actual value of the variable as an argument to a function is known as call by value (or) pass by value.
            In this mechanism, a copy of the value is passed from calling function to the called function.  Now, this value is stored temporarily in the formal argument of the called function.
At this stage, if we made any changes inside the function definition with the received value, those changes are not effect on the original variable.  Entire changes effected only on the formal arguments of the called function.

/* EXAMPLE PROGRAM FOR CALL BY VALUE */

#include<stdio.h>
#include<conio.h>
void swap(int,int);
main()
{
            int x,y;
            clrscr();
            printf(“\nEnter Two Values =”);
scanf(“%d%d’,&x,&y);
            printf("\nBefore swapping the values are: ");
            printf("\nx :%d, y :%d",x,y);
            change(x,y);
            printf("\nAfter swapping the values are: ");
            printf("\nx :%d, y:%d",x,y);
}
void swap(int p,int q)
{
            int temp;
            temp=p;
            p=q;
            q=temp;
}


2. Call by address:     The processing of passing address of the variable as an argument to a function is known as call by address (or) pass by address.
            In this mechanism, when we pass address of the variable as an argument to a function, the receiving argument at the called function must be a pointer variable.  When the pointer variable received the address, it points to the actual variable. 
At this stage, if made any changes inside the function definition; those changes are effected on the original variable.  Since, pointer variable points the original argument.

/* EXAMPLE PROGRAM FOR CALL BY ADDRESS */

#include<stdio.h>
#include<conio.h>
void swap(int *,int *);
main()
{
            int x,y;
            clrscr();
            printf(“\nEnter Two Values =”);
scanf(“%d%d’,&x,&y);
            printf("\nBefore swapping the values are: ");
            printf("\nx :%d, y :%d",x,y);
            change(&x,&y);
            printf("\nAfter swapping the values are: ");
            printf("\nx :%d, y:%d",x,y);
}
void swap(int *p,int *q)
{
            int temp;
            temp=*p;
            *p=*q;
            *q=temp;

}

No comments:

Post a Comment