You are on page 1of 11

Software Development Fundamentals (SDF) – I

ODD 2022

Lecture 29
Week 10

Jaypee Institute of Information Technology


(JIIT), 1
Outline – Lecture 29

1. Calling the Function


2. Call by Value / Pass by Value
3. Swapping of two numbers using Call by Value
4. Call by Reference / Pass by Reference
5. Swapping of two numbers using Call by Reference

2
Calling the Function
• Actual parameters: The parameters that appear in function calls.

• Formal parameters: The parameters that appear in function declarations.

• When we pass the actual parameters while calling a function then this is
known as function call by value.

• In this case the values of actual parameters are copied to the formal
parameters.

3
Example-1 Sum of three numbers
main( )
{
int a, b, c, sum ; • The variables a, b and c are called ‘actual
printf ( "\nEnter any three numbers " ) ; arguments’, whereas the variables x, y and z are
scanf ( "%d %d %d", &a, &b, &c ) ; called ‘formal arguments’.
• There are two methods of declaring the formal
sum = calsum ( a, b, c ) ; printf ( arguments. The one used in the program is known
"\nSum = %d", sum ) ; as Kernighan and Ritchie (or just K & R) method.
} calsum ( x, y, z )
int x, y, z ;
calsum ( x, y, z )
Another method is,
int x, y, z ; calsum ( int x, int y, int z )
{ This method is called ANSI method
int d ;

d=x+y+z;
return ( d ) ; output
Enter any three numbers 10 20 30
} Sum = 60
4
Call by Value
• In call by value method, the value of the actual parameters is copied into the
formal parameters.
• In this, we can not modify the value of the actual parameter by the formal
parameter.
• In call by value, different memory is allocated for actual and formal
parameters since the value of the actual parameter is copied into the formal
parameter.
• The actual parameter is the argument which is used in the function call
whereas formal parameter is the argument which is used in the function
definition.

5
Example 2 Swapping of two numbers using Call by Value
#include <stdio.h>
void swap(int , int); //prototype of the function int
main()

{
int a = 10;
int b = 20;
Output:
printf("Before swapping the values in main a = %d, b = %d\n“, a, b);
swap(a,b);
Before swapping the values in main a = 10, b = 20
printf("After swapping values in main a = %d, b = %d\n", a,b); After swapping values in function a = 20, b = 10
} After swapping values in main a = 10, b = 20
void swap (int a, int b)
{
int temp;
temp = a;
a=b;
b=temp;

printf("After swapping values in function a = %d, b = %d\n",a,b);


}

6
Example-3 Factorial of a number using Call by Value

main( )
{
int a, fact ;
printf ( "\nEnter any number " ) ; scanf
( "%d", &a ) ;
fact = factorial ( a ) ; output :
printf ( "Factorial value = %d", fact ) ;
} Enter any number 3
factorial ( int x ) Factorial value = 6
{
int f = 1, i ;
for ( i = x ; i >= 1 ; i-- )
f=f*i;
return ( f ) ;
}

7
Call by reference in C

•In call by reference, the address of the variable is passed into the
function call as the actual parameter.
•The value of the actual parameters can be modified by changing the
formal parameters since the address of the actual parameters is
passed.
•In call by reference, the memory allocation is similar for both formal
parameters and actual parameters. All the operations in the function
are performed on the value stored at the address of the actual
parameters, and the modified value gets stored at the same address.

8
Example 4 Swapping of two numbers using Call by Reference
#include <stdio.h>
void swap(int , int); //prototype of the function int main()

{
int a = 10; int b =
20;
printf ("Before swapping the values in main a = %d, b = %d\n“, a, b); swap(&a, Before
swapping the values in main a= 10, b= 20
&b); After swapping values in function a = 20, b = 10
After swapping values in main a = 20, b = 10
printf ("After swapping values in main a = %d, b = %d\n", a, b);
}
void swap (int *a, int *b)
{
int temp;
temp = *a;

*a=*b;
*b=temp;
printf("After swapping values in function a = %d, b = %d\n",*a,*b);
}

9
Call by value and Call by reference
in C
Original value

No Yes

Modified

Call by value Call by reference

10
Difference between call by value and call
by reference in C
Call by value Call by reference

Actual and formal arguments are created at the Actual and formal arguments are created at the
different memory location same memory location

A copy of the value is passed into the function An address of value is passed into the function

Changes made inside the function is limited to the Changes made inside the function validate outside
function only. The values of the actual parameters of the function also. The values of the actual
do not change by changing the formal parameters. parameters do change by changing the formal
parameters.

Values of variables are passes by Pointer variables are necessary to define to store
Simple technique. the address values of variables.

11

You might also like