Passing arguments in C functions

Argument passing is a technique for transferring data and information between the calling and called function. There are two ways to pass arguments to the called function:a)Call by Value b)Call by reference.

 

  Call by Value:

The call by value method is default technique of passing arguents.In this method only the copies of the caller’s variables are sent to the callee’s variables means a photocopy of actual arguments are passed to called function.

#include<stdio.h>

main()

{

int a=100,b=200;

clrscr();

change(a,b);

printf("\na=%d b=%d",a,b);

getch();

}

change(x,y)

int x,y;

{

int t;

t=x;

x=y;

y=t;

printf("\nx=%dy=%d",x,y);

}

call by value in c

Call by Reference(Address):

When the arguments listed in a function call are pointers, the memory address of the caller’s variables are transmitted to the function means the address of actual arguments of calling function are copied into formal arguments of called function.

#include<stdio.h>

main()

{

int a=100,b=200;

clrscr();

change(&a,&b);

printf("\na=%d b=%d",a,b);

getch();

}

change(int *x,int *y)

{

int *t;

*t=*x;

*x=*y;

*y=*t;

printf("\nx=%dy=%d",*x,*y);

}

call by reference

Explanation:

Three things that must be kept in mind while passing arguments are:

1)Number of arguments in the caller and callee function must be same.

2)Order of the arguments in caller and callee function should match.

3)The data type of actual and formal arguments should be same in both the caller and callee function. Any mismatch in the data type will produce unexpectedmresults.

About Us

Rekha SetiaRekha Setia is a passionate blogger of Extra Computer Notes. if you have any ideas or any request me @