Actual argument:
The arguments used in calling
functions are called as actual
arguments.
Formal argument:
The arguments used in called or user
defined functions are called as
formal arguments.
Call by value:
In this type the value of actual arguments
are passed to the formal arguments and the
operation is done on the formal arguments.
Any change made in the formal argument does
not effect actual arguments because formal
arguments are photocopy of actual
arguments. So when a function is called by
call by value method, it doesnt effect the
actual contents of the actual arguments
Changes made in the formal arguments are
local to the block of called function.
Once control return back to the calling
function the changes made vanish.
In call by value method the actual and
formal argument are stored at different
memory locations so both are not same.
The following program illustrate the
concept of call by value method.
#include<stdio.h>
#include<conio.h>
void swap(int , int );
void main()
{
int a=5,b=10;
clrscr();
printf("\nBefore swapping \n a = %d , b = %d",a,b);
swap(a,b); // a,b are actual argument
getch();
}
void swap(int a, int b) // this a and b are formal arguments.
{
int z;
z=a;
a=b;
b=z;
printf("\nIn swap function \n a = %d , b = %d",a,b);
}
OUTPUT:
Before swapping
a = 5 , b = 10
In swap function
a = 10 , b = 5
Call by reference:
In this type instead of passing
values,
addresses
are
passed.
Function operates on addresses rather
than
values.
Here
the
formal
arguments are pointers to the actual
arguments.
In
this
type
formal
argument
argument.
points
to
the
actual
Hence changes made in the argument
are permanent.
In call by reference the actual and formal
argument are pointing to the same memory
locations so both are same.
The following program illustrate the
concept of call by reference method.
#include<stdio.h>
#include<conio.h>
void swap(int * , int *);
void main()
{
int a=5,b=10;
clrscr();
printf("\nBefore swapping \n a = %d , b = %d",a,b);
swap(&a,&b);
getch();
}
void swap(int *a, int *b)
{
int z;
z=*a;
*a=*b;
*b=z;
printf("\nIn swap function \n a = %d , b = %d",*a,*b);
}
OUTPUT:
Before swapping
a = 5 , b = 10
In swap function
a = 10 , b = 5
// Sum of series using function with return type with argument
#include<stdio.h>
#include<conio.h>
int sum1(int );
void main()
{
int n,d;
printf(Enter number upto which u want to print series:);
scanf("%d",&n);
d=sum1(n);
printf("\nsum is %d",d);
getch();
}
int sum1(int n)
{
int i=1,sum=0;
while(i<=n)
{
sum = sum+i;
i++;
}
Return(sum1);
}