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

Call by Value Call by Referance

Call by value passes the value of an argument to the function. The function can change the copy but not the original variable. Call by reference passes the address of an argument. The function can change the original variable since it directly modifies the memory address. Call by reference is used to swap variables by dereferencing the address pointers to assign new values directly to the original variables.

Uploaded by

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

Call by Value Call by Referance

Call by value passes the value of an argument to the function. The function can change the copy but not the original variable. Call by reference passes the address of an argument. The function can change the original variable since it directly modifies the memory address. Call by reference is used to swap variables by dereferencing the address pointers to assign new values directly to the original variables.

Uploaded by

rohitggothi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Call by Value

&
Call by Reference

Function
C Prog
Call by Value & Call by Reference

A function is a block of statements that perform a particular task.


The parameters of functions can be passed in two ways:
#include <stdio.h>
void main()
{
int s,a,b;
int fnadd(int,int);
printf("\n Enter two values : "); 1.Call by value: A copy of the
scanf("%d",&a); variable is passed to the function.
scanf("%d",&b);

Address of
s=fnadd(a,b); a 1004 a=100

Address of b=200
} b 1016
int fnadd(int x,int y)
{ Address of x=100
int temp; x 3000
temp=x+y; Address of y=200
return temp; x 3020
}
#include <stdio.h>
void main()
{
int s,a,b;
int fnadd(int *,int *); 2. Call by reference: An address
printf("\n Enter two values : "); of the variable is passed to the function.
scanf("%d",&a);
scanf("%d",&b);
s=fnadd(&a, &b);
printf("\n Sum of given two values : =%d",s); Address of
a 1004 a=100
}
Address of b=200
int fnadd(int *x,int *y) b 1016
{
int temp; Address of 1004
temp=*x+*y; x 3000
return temp; Address of 1016
} x 3020
swapping of two numbers
Using call by value
#include<stdio.h>

int main() {
int a = 20;
int b= 68;
void swap(int, int); a=100
printf("The numbers before swapping a and b are %d %d \n",a, b);
b=200
swap(a, b);
printf("The numbers after swapping a and b are %d %d \n",a, b);
return 0;
} x=100 y=200
void swap(int x, int y)
y=200 x=100
{
int temp = x;
x = y;
y = temp;

}
swapping of two numbers
Using call by Reference
#include<stdio.h>

int main() { Address of


a=100 a=200
int a = 20; a 1004
int b= 68; Address of b=200 b=100
void swap(int *, int *); b 1016
printf("The numbers before swapping a and b are %d %d \n",a, b);
swap(&a, &b); Address of 1004
printf("The numbers after swapping a and b are %d %d \n",a, b); x 3000
return 0; 1016
Address of
} x 3020
void swap(int *x, int *y)
{
int temp = *x;
*x = *y;
*y = temp;

You might also like