0% found this document useful (0 votes)
20 views

Functions

The document discusses call by value and call by reference in functions. In call by value, the actual arguments are passed by value to the formal arguments, so changes to formal arguments are not reflected in the actual arguments. In call by reference, the addresses of actual arguments are passed to formal arguments, so changes to formal arguments are reflected in the actual arguments as they both refer to the same memory location. Examples are provided to illustrate call by value and call by reference. The document also discusses calculating the sum of a series using a function with a return type and argument.

Uploaded by

Tribhuwan Pant
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Functions

The document discusses call by value and call by reference in functions. In call by value, the actual arguments are passed by value to the formal arguments, so changes to formal arguments are not reflected in the actual arguments. In call by reference, the addresses of actual arguments are passed to formal arguments, so changes to formal arguments are reflected in the actual arguments as they both refer to the same memory location. Examples are provided to illustrate call by value and call by reference. The document also discusses calculating the sum of a series using a function with a return type and argument.

Uploaded by

Tribhuwan Pant
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

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);
}

You might also like