Search This Blog

Saturday, March 19, 2011

there are two ways to call a function
1.call by value

2.call by function


in call by value variables are sent simply
lets see the example
int a=1,b=2;
func(a,b);

here func is function name and a and b are arguments and here values (a and b) are sent as simple agrument

in call by reference address of a variable is sent while a calling a fuction and in function body we will use of pointer variable to access the value

lets see the example

int a=1,b=2;
int *p,*ptr
p=&a;
ptr=&b;
func(p,ptr);

we can use following also for same task

int a=1,b=2;
func(&a,&b);

here address of variable is sent thats why it is known as call by address

now see the function body,will used in call by reference

void func(int *x, int *y)
{

statement
statement
statement

}

                                 programme for call by reference

#include<stdio.h>
#include<conio.h>
void func(int *,int *); /*function prototype*/ 
void main()
{
int a,b;
a=1;
b=2;
func(&a,&b);
getch();
}
void func(int *x, int *y);
{
int t;
t=*y;
*y=*x;
*x=t;
}

No comments:

Post a Comment