• Skip to main content
  • Skip to primary sidebar
  • Skip to secondary sidebar
  • Skip to footer

Computer Notes

Library
    • Computer Fundamental
    • Computer Memory
    • DBMS Tutorial
    • Operating System
    • Computer Networking
    • C Programming
    • C++ Programming
    • Java Programming
    • C# Programming
    • SQL Tutorial
    • Management Tutorial
    • Computer Graphics
    • Compiler Design
    • Style Sheet
    • JavaScript Tutorial
    • Html Tutorial
    • Wordpress Tutorial
    • Python Tutorial
    • PHP Tutorial
    • JSP Tutorial
    • AngularJS Tutorial
    • Data Structures
    • E Commerce Tutorial
    • Visual Basic
    • Structs2 Tutorial
    • Digital Electronics
    • Internet Terms
    • Servlet Tutorial
    • Software Engineering
    • Interviews Questions
    • Basic Terms
    • Troubleshooting
Menu

Header Right

Home » C » Call by value and Call by reference in C
Next →
← Prev

Call by value and Call by reference in C

By Dinesh Thakur

There are two parameter passing methods passing (or calling) data to functions in C language, i.e., Call by Value or Call by Reference (also Called pass by value and pass by reference). The most significant distinction between call by value and call by reference is that in the call by value, passing an actual value of the parameter. While, in a call by reference, passing the reference of the parameter; hence some change made to formal arguments will also reflect in actual arguments.

Let’s understand call by value and call by reference in c language.

Note: The parameters passed to function will be called actual parameters, whereas the parameters obtained from the function are called formal parameters.

In C, all function arguments are passed “by value” because C doesn’t support references as C++ and Java do. In C, both the calling and called functions don’t share any memory they have their copy, along with the called function can’t directly change a variable in the calling function; it may only alter its private, temporary copy.

In C, we use pointers to attain call by reference. In C++, we could use pointers or references to for pass by reference. In Java, primitive types passed as values, and non-primitive types are constant references.

Call by value and call by reference in C

We’ll be covering the following topics in this tutorial:

  • Call by value in C
  • Call by reference in C
  • Difference between call by value and call by reference in C
  • When to Use Call by Value and When to use Call by Reference?
  • Advantages of using Call by value method
  • Advantages of using Call by reference method
  • Disadvantages of using Call by value method
  • Disadvantages of using Call by reference method

Call by value in C

• In call by value, a copy of actual arguments passed to formal arguments and the two types of parameters stored in different stack memory locations. So any changes made inside functions parameter, it is changed for the current function only. It will not change the value of a variable inside the caller method such as main().
• In call by value function, we can’t alter the value of the actual parameter from the formal parameter.

• In call by value, distinct memory allocated to actual and formal parameters because the value of the actual parameter replicated into the formal parameter.

• The actual parameter is the argument that’s used at the function call, whereas the formal parameter is the argument used in the function definition.

Consider the following example for the Call by Value in C

If the parameter passed by value, the parameter replicated from the variable used in, for example, main() into a variable used by the function. So if the parameter passed altered within the function, the value only changed from the variable used within the function. Let’s take us to look at a call by value example:

#include <stdio.h>
void pass_by_value(int p) {
  printf("Inside pass_by_value p = %d before adding 10.\n", p);
  p += 10;
  printf("Inside pass_by_value p = %d after adding 10.\n", p);
}
int main() {
  int Var = 10;
  printf("Var = %d before function pass_by_value.\n", Var);
  pass_by_value(Var);
  printf("Var = %d after function pass_by_value.\n", Var);
  return 0;
}
 The output of this call by value code example: 
Var = 10 before function pass_by_value.
Inside pass_by_value p = 10 before adding 10.
Inside pass_by_value p = 20 after adding 10.
Var = 10 after function pass_by_value.

Let us take a look at what is going on within this pass-by-value source code example. From the main() we create an integer with the value of 10. We print some information at each point, starting by printing our variable Var. Then function pass_by_value is called, and we enter the variable Var. This variable (Var) subsequently copied to the function variable p. At the function, we add 10 to p (and call some print statements). Then when another statement called in main() the value of variable Var printed. We can understand that the value of variable Var isn’t affected by the call of the function pass_by_value().

Call by reference in C

• Simply call by reference method, the location (address) of the variable passed to the function call as the actual parameter.

• The value of the actual parameters could be altered by altering the formal parameters because the address of the actual parameters passed.

• In call by reference method, Both the actual and formal parameters refer to the same locations. Any changes made inside function performed on the value stored at the address of the actual parameters and the modified value gets stored at the same address.

Consider the following example for the call by reference.

If data is passed by reference, a pointer to the data is copied instead of the actual variable as is done in a call by value. Because a pointer is copied, if the value at that pointers address is changed in the function, the value is also changed in main(). Let’s take a look at a code example:

#include <stdio.h>
void pass_by_reference(int *p) {
  printf("Inside pass_by_reference p = %d before adding 10.\n", *p);
  (*p) += 10;
  printf("Inside pass_by_reference p = %d after adding 10.\n", *p);
}
int main() {
  int Var = 10;
  printf("Var = %d before function pass_by_reference.\n", Var);
  pass_by_reference(&amp;amp;amp;amp;amp;amp;amp;Var);
  printf("Var = %d after function pass_by_reference.\n", Var);
  return 0;
}

The output of this call by reference source code example will look like this:

Var = 10 before function pass_by_reference.
Inside pass_by_reference p = 10 before adding 10.
Inside pass_by_reference p = 20 after adding 10.
Var = 20 after function pass_by_reference.

Let’s explain what’s going on within this source code example. We begin with an integer Var with the value 10. The function pass_by_reference() called, and also the address of the variable Var passed to the function. In a function, there’s some before and after a print statement is completed and there is 10 added to value at memory pointed by p. Thus, at the end of the function, value is 20. Then in main(), we print the variable Var, and as you can see, the value is altered (as expected) to 20.

Difference between call by value and call by reference in C

Difference Between Call by value and call by reference

Call By ValueCall by Reference
A copy of the variable passed into the function. Such functions are called “Call By Values”.An address of variables(location of variables) passed into the function. Such functions called “Call By References”.
Parameters passed by calling functions don’t change by altering by called functions.Called functions may alter parameters passed by calling functions.
Actual and formal arguments created at a different memory location.Actual and formal arguments created at the same memory location.
It is the default method in programming languages.It is supported only Java language.
It uses a straightforward method for passing the variable value.It uses pointers to store the address of variables.
Call by value does not modify original value.Call by reference modify original value.
In the Call by value method, Actual arguments stay safe as they can’t be altered accidentally.In the Call by reference method, Actual arguments aren’t Safe. They may be unintentionally altered, and that means you have to take care of arguments operations attentively.

When to Use Call by Value and When to use Call by Reference?

One Benefit of the call by reference method is that it is using pointers, so there’s is no doubling of the memory used by the variables (like the copy of the call by value method). It’s, naturally, good, lowering the memory footprint is almost always a good thing. So why don’t we create all the parameters call by reference?

There are two reasons why this isn’t a good idea, and you have to select between call by value and call by reference. The reason is the side Impacts and privacy. Unwanted side effects are often due to accidentally changes that made into a call by reference parameter. Additionally, typically, you would like the data to be private and that someone calling a function can change if you want it. So it’s better to use a call by value by default and only use call by reference if data changes expected.

Advantages of using Call by value method

• The method does not alter the original value.; therefore, it’s keeping data.

• Whenever a function is called it, never affect the real contents of the actual arguments.

• Value of actual arguments passed into the formal arguments; therefore, any modifications made in the formal argument doesn’t impact the actual instances.

Advantages of using Call by reference method

• The function may alter the value of the argument, which can be very helpful.
• It doesn’t create replicate data for holding only one value which can help you to conserve memory space.
• Inside this method, there isn’t any copy of the argument created. Therefore it’s processed extremely fast.
• Helps you to avoid alterations done by mistake.
• A individual studying the code never understands that the value could be modified at the function.

Disadvantages of using Call by value method

• Changes to actual parameters may also alter corresponding argument variables.
• Inside this method, arguments should be variables.
• You can not directly alter a variable in a function body.
• Sometime argument may be complicated expressions.
• You will find two copies made for the same variable that’s is not memory efficient.

Disadvantages of using Call by reference method

• Powerful non-null guarantee. A function taking at a reference ought to be certain the input is non-null. Therefore, the null check does not need to be made.

• Passing by reference makes the function maybe not pure theoretically.

• A lifetime warranty is a large issue with references. It can be especially risky when working with lambdas and multi-threaded programs.

You’ll also like:

  1. Difference Between Call by Value and Call By reference in Java
  2. Call by Value and Call by Reference
  3. C++ function Call by Reference
  4. Function call by reference in C
  5. C Program Interchange Values of 2 no: using Call By Reference
Next →
← Prev
Like/Subscribe us for latest updates     

About Dinesh Thakur
Dinesh ThakurDinesh Thakur holds an B.C.A, MCDBA, MCSD certifications. Dinesh authors the hugely popular Computer Notes blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps.

Dinesh Thakur is a Freelance Writer who helps different clients from all over the globe. Dinesh has written over 500+ blogs, 30+ eBooks, and 10000+ Posts for all types of clients.


For any type of query or something that you think is missing, please feel free to Contact us.


Primary Sidebar

C Programming

C Programming Tutorials

  • C - History
  • C - Anatomy
  • C - Constants
  • C - Identifiers
  • C - Data Types
  • C - Libraries File
  • C - Header Files
  • C - Basic Language
  • C - Data Types Sizes
  • C - Header Files Importance
  • C - Escape Sequences
  • C - Main() Purpose
  • C - Program Procedure
  • C - Control Statements
  • C - Enumeration Constant
  • C - Add numbers
  • C - Return Statement
  • C - Avoid Goto
  • C - Command Line Arguments
  • C - Switch Case
  • C - Switch Case Limitations
  • C - getchar() and putchar()
  • C - Iteration Statements
  • C - Pass by Value and Reference
  • C - Structures and Unions
  • C - Structure
  • C - Dynamic Memory
  • C - Fgets and Fputs Functions
  • C - Gets() and Puts() Functions
  • C - Armstrong Number
  • C - Storage Classes
  • C - Fibonacci Series
  • C - Precision Setting
  • C - const Parameters

C - Variable & It's Type

  • C - Variables
  • C - Variable Lifetime
  • C - Static Variable
  • C - Register Variable
  • C - Global Variables
  • C - Auto Variables
  • C - Local Variables

C - Operator & Expressions

  • C - Operator
  • C - Boolean Operators
  • C - Bitwise Operator
  • C - Arithmetic Operators
  • C - Modulus Operator
  • C - Ternary Operator
  • C - Expressions
  • C - Arithmetic Expressions

C - Array

  • C - Arrays
  • C - Array Types
  • C - Array Characteristics
  • C - Static Arrays
  • C - Global Arrays
  • C - 3D Arrays
  • C - Dynamic Arrays
  • C - Pointer to 3D Arrays
  • C - Array Elements Hold
  • C - Arrays as Function Parameters
  • C - Accessing Matrix Elements
  • C - File Handling
  • C - Matrix Multiplication
  • C - Dynamic Memory Allocation

C - Searching & Sorting

  • C - Data Structures
  • C - Linear Search
  • C - Bubble Sort
  • C - Merge Sort
  • C - Linked List
  • C - Insertion Sort
  • C - Binary Search
  • C - Selection Sort
  • C - Quick Sort

C - Functions

  • C - Functions
  • C - Functions Advantages
  • C - Void Functions
  • C - Function Call
  • C - Default Return Value
  • C - String functions

C - Pointer

  • C - Pointers
  • C - Type Casting Of Pointers
  • C - Pointer Advantages
  • C - Pointers Initialization
  • C - Vectors and Pointers

C - Differences

  • C - C Vs C++
  • C - Formal Args. Vs Actual Args.
  • C - Keywords Vs Identifiers
  • C - Strings Vs Character Arrays
  • C - Address Vs Dereference Operator
  • C - Goto Vs longjmp
  • C - Declaring Vs Defining Variable
  • C - String Vs Array
  • C - Call by Value Vs Reference
  • C - Structure Vs Union
  • C - For Vs While loops
  • C - Compiler Vs Interpreter

C - Programs

  • C Program Standard Deviation
  • C Program Calculate Tax
  • C Program Sum Series
  • C Program Merge Arrays
  • C Program Euclid’s Algorithm
  • C Program Print Weekdays
  • C Program Sum of Digits
  • C Program Print a list
  • C Program Print Pythagorean
  • C Program Quiz program
  • C Program Display Table
  • C Program Print Comma-Separated
  • C Program Prints Prime Numbers
  • C Program for Print Integer
  • C Program Count Number
  • C Program Print Color Name
  • C Program Print Odd Numbers
  • C Program Calculate area
  • C Program for a Menu
  • C Program Add Two Vectors
  • C Program Array Addresses
  • C Program Division by Zero Error
  • C Program Compare two Dates
  • C Program Tower of Hanoi
  • C Program return 3 Numbers
  • C Program for Prime Numbers
  • C Program for Factorial
  • C Program for Palindrome

Other Links

  • C Programming - PDF Version

Footer

Basic Course

  • Computer Fundamental
  • Computer Networking
  • Operating System
  • Database System
  • Computer Graphics
  • Management System
  • Software Engineering
  • Digital Electronics
  • Electronic Commerce
  • Compiler Design
  • Troubleshooting

Programming

  • Java Programming
  • Structured Query (SQL)
  • C Programming
  • C++ Programming
  • Visual Basic
  • Data Structures
  • Struts 2
  • Java Servlet
  • C# Programming
  • Basic Terms
  • Interviews

World Wide Web

  • Internet
  • Java Script
  • HTML Language
  • Cascading Style Sheet
  • Java Server Pages
  • Wordpress
  • PHP
  • Python Tutorial
  • AngularJS
  • Troubleshooting

 About Us |  Contact Us |  FAQ

Dinesh Thakur is a Technology Columinist and founder of Computer Notes.

Copyright © 2025. All Rights Reserved.

APPLY FOR ONLINE JOB IN BIGGEST CRYPTO COMPANIES
APPLY NOW