Passing arguments to function using pointers


Call by value method is use to passing parameters to a function,Using call by method,it is impossible to modify the actual parameters in the call when you pass them to a function.The incoming arguments to a function are treated as local variables in the function and those local variables get copy of the values passed from their caller.Pointer provide a mechanism to modify data declared in one function using code written in another function.The calling function send the address of the variables and called function must declare those incoming arguments as pointers.In order to modify the variables sent by the caller,the called function must dereference the pointers that were passed to it.Thus,passing pointers to a function avoids the overhead of copying data from one function to another.


Write a program to add two integers using functions 


#include<stdio.h>
#include<conio.h>
void sum(int *a,int *b,int *t);
void main()
{
int num1,num2,total;
printf("\n Enter the first number:");
scanf("%d",&num);
printf("\n Enter the second number:");
scanf("%d",&num);
sum(&num1,&num2,&total);
getch();
}
void sum(int *a,int *b,int *t)
{
*t=*a+*b;
}


Output

Enter the first number:2

Enter the first number:3

total=5

Comments