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

Function Part22 PDF

This document discusses parameter passing methods in C functions. It explains call by value and call by reference by providing examples of swapping two numbers using each method. Call by value passes the actual value of an argument into the formal parameter. Changes made to the parameter inside the function are not reflected in the actual argument. Call by reference passes the address of the argument. This allows the function to make changes to the original argument by accessing it through a pointer. The examples demonstrate that call by reference swaps the original values, while call by value only swaps the local copies inside the function.

Uploaded by

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

Function Part22 PDF

This document discusses parameter passing methods in C functions. It explains call by value and call by reference by providing examples of swapping two numbers using each method. Call by value passes the actual value of an argument into the formal parameter. Changes made to the parameter inside the function are not reflected in the actual argument. Call by reference passes the address of the argument. This allows the function to make changes to the original argument by accessing it through a pointer. The examples demonstrate that call by reference swaps the original values, while call by value only swaps the local copies inside the function.

Uploaded by

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

EST102 Programming in C

MODULE –IV
Functions

VIVITHA VIJAY
ASSISTANT PROFESSOR
DEPARTMENT OF CSE
SRI VELLAPPALLY NATESAN COLLEGE OF
ENGINEERING
[MGIT]
Parameter(argument) passing methods
CALL BY VALUE
(OR)
PASS BY VALUE
Swapping of two numbers using call by value
#include<stdio.h>
void swap(int a,int b) → Formal parameters
{ a b
int temp;
10 20
temp=a;
a= b; 4000 4004
b=temp; a b
printf(“values inside function: a=%d, b=%d",a,b);
} 20 10
void main()
{ 4000 4004
int x,y;
printf("Enter the numbers:");
scanf("%d%d",&x,&y);
printf("Before swapping: x=%d, y=%d",x,y);
swap(x,y); → actual parameters x y
printf("After swapping : x=%d, y=%d",x,y);
10 20
}
Output
Enter the numbers:10 20 2000 2004
Before swapping: x=10 y=20
Values inside function : a=20 ,b=10
CALL BY REFERENCE
(OR)
PASS BY REFERENCE
Swapping of two numbers using call by reference
#include<stdio.h>
void swap(int *a,int *b) → Formal parameters
{ a b
int temp;
2000 2004
temp=*a;
*a=* b; 4000 4004
*b=temp; x y
printf(“values inside function:a=%d, b=%d",*a,*b);
} 20 10
void main()
{ 2000 2004
int x,y;
printf("Enter the numbers:");
scanf("%d%d",&x,&y);
printf("Before swapping: x=%d, y=%d",x,y);
swap(&x,&y); → actual parameters x y
printf("After swapping : x=%d, y=%d",x,y);
} 10 20
Output
Enter the numbers:10 20 2000 2004
Before swapping: x=10 y=20
Values inside function : a=20 ,b=10
Differences between call by value and call
by reference
Thank you

You might also like