0% found this document useful (0 votes)
43 views2 pages

CS-2 Call by Value ND Reference

This document discusses pass by value versus pass by reference in C programming. When a function is passed a value by value, a copy of the value is passed rather than the variable itself, so any changes to the parameter inside the function do not affect the original variable. Pass by reference passes the address of the variable, allowing the function direct access and any changes made will affect the original variable. The document provides examples to demonstrate pass by value versus pass by reference and discusses how understanding pointers is necessary to fully understand pass by reference.

Uploaded by

Santhini Ka
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views2 pages

CS-2 Call by Value ND Reference

This document discusses pass by value versus pass by reference in C programming. When a function is passed a value by value, a copy of the value is passed rather than the variable itself, so any changes to the parameter inside the function do not affect the original variable. Pass by reference passes the address of the variable, allowing the function direct access and any changes made will affect the original variable. The document provides examples to demonstrate pass by value versus pass by reference and discusses how understanding pointers is necessary to fully understand pass by reference.

Uploaded by

Santhini Ka
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Modular C Programming and Function Pass by value Vs Pass by reference

Consider this program

#include<stdio.h>

void add_One(int);

main() {
int b = 10;
add_One(b);
printf(Variable b in function main() has value: %d\n, b);
}

void add_One (int num) {


num = num + 1;
printf(Variable num in function add_One() has value: %d\n, num);
}

Output
Variable num in function add_One() has value: 11
Variable b in function main() has value: 10

When the main() function call the add_One() function, it passes in the
VALUE of variable b by making a COPY of it and assigned it to the formal
parameter, num . Do not confused that the variable b is passed into the
function; only a copy of its value is passed into the function. Therefore,
whatever changes made to the num in add_One() function, does not change
the value of variable b in main() function.

This kind of parameter passing mechanism is called Pass By Value.

There is another kind of parameter passing mechanism where the


ADDRESS (instead of the value) of a variable is passed into the function. In
this case, the function has a direct access to the variable and hence, any
changes made will directly effect the value in the variable.

This kind of parameter passing mechanism is called Pass By Reference.

Pass by value Vs Pass by reference 1


In order to understand pass by reference, it is compulsory to understand the
concept of pointer.(Discuss soon)

Exercise
1. Whats the output?

#include<stdio.h>

void square(int);

main() {
int num = 3;
square(num);
printf(num: %d\n, num);
}

void square (int num) {


num = num * num;
}
2. Whats the output?

#include<stdio.h>

int square(int);

main() {
int num = 3;
square(num);
printf(num: %d\n, num);
num = square(num);
printf(num: %d\n, num);
}

int square (int num) {


num = num * num;
return num;
}

Pass by value Vs Pass by reference 2

You might also like