Codzify - Call by Value and Call by Reference in C

3 min 52 sec read Basic


In case of callByValue value will not change after calling a function but in case of callByReference value will change after calling a function .

CallByValue just changes the value of the local variable. Local variables are the variables declared inside the block of function. So when we change the value inside a function there is a reflection but in the main function, it's not.

Whereas, CallByReference changes the value at the memory address. So when we change the value inside the function value gets directly changed at the address of that variable. That's why you see the reflection .

Call By Value

#include 
void change(int a);
void main()
{
int a = 5; 
printf("Value of a before calling Function = %d",a ); // 5
change(a);     /* Call by Value */ 
printf("Value of a after calling Function = %d",a ); // 5
}

void change (int a)
{
printf("Value of a before changing value of a = %d",a ); // 5 
a = 6;    /* Lets change the value of a*/
printf("Value of a after changing value of a = %d",a ); // 6 
}

Output

Value of a before calling Function = 5
Value of a before changing value of a = 5
Value of a after changing value of a = 6
Value of a after calling Function = 5

Here as you see we changed the value of 'a' inside Change function as 6. But it's not reflected inside main Function.

Call By Reference

Example of Call by Reference :-

#include <stdio.h>
void change(int *a);
void main()
{
int a = 5; 
printf("Value of a before calling Function = %d",a ); // 5
change(&a);    /* Call by Reference */  
printf("Value of a after calling Function = %d",a ); // 6
  }


void change (int *a )
{
printf("Value of a before changing value of a = %d",*a ); // 5 
*a = 6;      /* Lets change the value of a*/
printf("Value of a after changing value of a = %d",*a ); // 6
}

Output

Value of a before calling Function = 5
Value of a before changing value of a = 5
Value of a after changing value of a = 6
Value of a after calling Function = 6

Here as you see we changed the value of 'a' inside Change function as 6. Yes it is reflected inside main Function.

Why so?

In case of callByValue value is not changed after calling a function but in case of callByReference value is changed after calling a function .

CallByValue just changes the value of the local variable. Local variables are the variables declared inside the block of function. So when we changed the value of 'a' inside a function there is a reflection but in the main function, it's not.

Whereas, CallByReference changes the value at the memory address. So when we change the value of 'a' inside the function value gets directly changed at the address of that variable. That's why you see the reflection.

 

Codzify's

1:1 & 1:Many Mentorship Program

Be job-ready. Learn how to crack the interviews with industry ready training program.


Learn More

Data Structures & Algorithms

What is Dynamic Programming and how to use it

This is a quick introduction to Dynamic Programming.

3 ways to check anagrams

Let's discuss the 3 ways to check anagrams and their time complexity.

Bubble Sort Algorithm

Topics covered: 1) Basics of Bubble Sort Algorithm 2) Worst Case, Best Case complexity

Introduction to Trees

What is a Tree and Terminologies like root, sibling, parent node, children, Leaf node, Ancestor & Descendant, Properties of Tree Data Structure, Depth of node, Height of node

Address Calculation in 2d array

Let's discuss about the Address Calculation using row-major order & column-major order.

Javascript

Javascript - Postfix Vs Prefix

Let's discuss the difference between postfix and prefix with a practical example.

Javascript Identifiers, Variables

In this session, we will discuss the concept of identifiers, variables and operators in javascript with a practical example.

Bootstrap

Bootstrap 4 Tutorial: Modal Popups Made Easy | Live Demo

Let's discuss the Modal Popups in Bootstrap.

How to create a dropdown list in navbar bootstrap

In this video, we will discuss How to create a dropdown list in navbar bootstrap with a practical example.

Navigation tabs in Bootstrap

Let's discuss about the navigation tabs in Bootstrap

Bootstrap Buttons

Let's discuss about the button classes in bootstrap.


Recommended tutorials