FUNCTION CALL METHODS

C Supports two types of calling a Function :

1. Call by Value
2. Call by Address

ARGUMENT PASSING – CALL BY VALUE

In this type, value of actual arguments are passed to the formal arguments and the operation is done on the formal arguments. Any changes made in the formal arguments does not affect the actual arguments because formal arguments are photocopy of actual arguments. Changes made in the formal arguments are local to the block of called-function. Once control returns back to the calling-function the changes made vanish.

 Program to send values using call-by-value method.

#include <stdio.h>
void main()
{
int x,y,
printf(“enter values of x & y : “);
scanf(“%d %d “, &x, &y);
printf(“\n old values x=%d y =%d”, x, y);
change(x,y) ;
printf(“\n new values x=%d y =%d”, x, y);
}
void change(int a, int b)
{
k=a; a=b; b=k;
return;
}

ARGUMENT PASSING – CALL BY ADDRESS VALUE

The Address of actual parameters are copied to formal parameters. Here changing the formal parameters indirectly affects the actual parameters.

#include<stdio.h>
#include<conio.h>
void swap(int *, int *);
int main()
{ int a, b; clrscr();
printf(“\n\tEnter Values of a and b: “); scanf(“%d%d”,&a,&b);
printf(“\n\t Before SWAP a = %d b = %d”, a, b);
swap(&a, &b);
printf(“\n\t After SWAP a = %d b = %d”, a, b);
getch(); return 0;
}
void swap(int *x, int *y)
{
int t;
t = *x;
*x = *y;
*y = t;
}
FUNCTION CALL METHODS FUNCTION CALL METHODS Reviewed by Unknown on 04:46 Rating: 5

No comments:

Powered by Blogger.