- C Programming Tutorial
- C - Home
- Basics of C
- C - Overview
- C - Features
- C - History
- C - Environment Setup
- C - Program Structure
- C - Hello World
- C - Compilation Process
- C - Comments
- C - Tokens
- C - Keywords
- C - Identifiers
- C - User Input
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Integer Promotions
- C - Type Conversion
- C - Type Casting
- C - Booleans
- Constants and Literals in C
- C - Constants
- C - Literals
- C - Escape sequences
- C - Format Specifiers
- Operators in C
- C - Operators
- C - Arithmetic Operators
- C - Relational Operators
- C - Logical Operators
- C - Bitwise Operators
- C - Assignment Operators
- C - Unary Operators
- C - Increment and Decrement Operators
- C - Ternary Operator
- C - sizeof Operator
- C - Operator Precedence
- C - Misc Operators
- Decision Making in C
- C - Decision Making
- C - if statement
- C - if...else statement
- C - nested if statements
- C - switch statement
- C - nested switch statements
- Loops in C
- C - Loops
- C - While loop
- C - For loop
- C - Do...while loop
- C - Nested loop
- C - Infinite loop
- C - Break Statement
- C - Continue Statement
- C - goto Statement
- Functions in C
- C - Functions
- C - Main Function
- C - Function call by Value
- C - Function call by reference
- C - Nested Functions
- C - Variadic Functions
- C - User-Defined Functions
- C - Callback Function
- C - Return Statement
- C - Recursion
- Scope Rules in C
- C - Scope Rules
- C - Static Variables
- C - Global Variables
- Arrays in C
- C - Arrays
- C - Properties of Array
- C - Multi-Dimensional Arrays
- C - Passing Arrays to Function
- C - Return Array from Function
- C - Variable Length Arrays
- Pointers in C
- C - Pointers
- C - Pointers and Arrays
- C - Applications of Pointers
- C - Pointer Arithmetics
- C - Array of Pointers
- C - Pointer to Pointer
- C - Passing Pointers to Functions
- C - Return Pointer from Functions
- C - Function Pointers
- C - Pointer to an Array
- C - Pointers to Structures
- C - Chain of Pointers
- C - Pointer vs Array
- C - Character Pointers and Functions
- C - NULL Pointer
- C - void Pointer
- C - Dangling Pointers
- C - Dereference Pointer
- C - Near, Far and Huge Pointers
- C - Initialization of Pointer Arrays
- C - Pointers vs. Multi-dimensional Arrays
- Strings in C
- C - Strings
- C - Array of Strings
- C - Special Characters
- C Structures and Unions
- C - Structures
- C - Structures and Functions
- C - Arrays of Structures
- C - Self-Referential Structures
- C - Lookup Tables
- C - Dot (.) Operator
- C - Enumeration (or enum)
- C - Structure Padding and Packing
- C - Nested Structures
- C - Anonymous Structure and Union
- C - Unions
- C - Bit Fields
- C - Typedef
- File Handling in C
- C - Input & Output
- C - File I/O (File Handling)
- C Preprocessors
- C - Preprocessors
- C - Pragmas
- C - Preprocessor Operators
- C - Macros
- C - Header Files
- Memory Management in C
- C - Memory Management
- C - Memory Address
- C - Storage Classes
- Miscellaneous Topics
- C - Error Handling
- C - Variable Arguments
- C - Command Execution
- C - Math Functions
- C - Static Keyword
- C - Random Number Generation
- C - Command Line Arguments
- C Programming Resources
- C - Questions & Answers
- C - Quick Guide
- C - Cheat Sheet
- C - Useful Resources
- C - Discussion
Function Call by Value in C
In C language, a function can be called from any other function, including itself. There are two ways in which a function can be called − (a) Call by Value and (b) Call by Reference. By default, the Call by Value mechanism is employed.
Formal Arguments and Actual Arguments
You must know the terminologies to understand how the call by value method works −
Formal arguments − A function needs certain data to perform its desired process. When a function is defined, it is assumed that the data values will be provided in the form of parameter or argument list inside the parenthesis in front of the function name. These arguments are the variables of a certain data type.
Actual arguments − When a certain function is to be called, it should be provided with the required number of values of the same type and in the same sequence as used in its definition.
Take a look at the following snippet −
type function_name(type var1, type var2, ...)
Here, the argument variables are called the formal arguments. Inside the function’s scope, these variables act as its local variables.
Consider the following function −
int add(int x, int y){ int z = x + y; return z; }
The arguments x and y in this function definition are the formal arguments.
Example: Call by Value in C
If the add() function is called, as shown in the code below, then the variables inside the parenthesis (a and b) are the actual arguments. They are passed to the function.
Take a look at the following example −
#include <stdio.h> int add(int x, int y){ int z = x + y; return z; } int main(){ int a = 10, b = 20; int c = add(a, b); printf("Addition: %d", c); }
Output
When you run this code, it will produce the following output −
Addition: 30
The Call by Value method implies that the values of the actual arguments are copied in the formal argument variables. Hence, "x" takes the value of "a" and "b" is assigned to "y". The local variable "z" inside the add() function stores the addition value. In the main() function, the value returned by the add() function is assigned to "c", which is printed.
Note that a variable in C language is a named location in the memory. Hence, variables are created in the memory and each variable is assigned a random memory address by the compiler.
How Does "Call by Value" Work in C?
Let us assume that the variables a, b and c in the main() function occupy the memory locations 100, 200 and 300 respectively. When the add() function is called with a and b as actual arguments, their values are stored in x and y respectively.
The variables x, y, and z are the local variables of the add() function. In the memory, they will be assigned some random location. Let's assume that they are created in memory address 1000, 2000 and 3000, respectively.
Since the function is called by copying the value of the actual arguments to their corresponding formal argument variables, the locations 1000 and 2000 which are the address of x and y will hold 10 and 20, respectively. The compiler assigns their addition to the third local variable z which is returned.
As the control comes back to the main() function, the returned data is assigned to c, which is displayed as the output of the program.
Example
Call by Value is the default function calling mechanism in C. It eliminates a function’s potential side effects, making your software simpler to maintain and easy to understand. It is best suited for a function expected to do a certain computation on the argument received and return the result.
Here is another example of Call by Value −
#include <stdio.h> /* function declaration */ void swap(int x, int y); int main(){ /* local variable definition */ int a = 100; int b = 200; printf("Before swap, value of a: %d\n", a); printf("Before swap, value of b: %d\n", b); /* calling a function to swap the values */ swap(a, b); printf("After swap, value of a: %d\n", a); printf("After swap, value of b: %d\n", b); return 0; } void swap(int x, int y){ int temp; temp = x; /* save the value of x */ x = y; /* put y into x */ y = temp; /* put temp into y */ return; }
Output
When you run this code, it will produce the following output −
Before swap, value of a: 100 Before swap, value of b: 200 After swap, value of a: 100 After swap, value of b: 200
It shows that there are no changes in the values, although they had been changed inside the function.
Since the values are copied in different local variables of another function, any manipulation doesn’t have any effect on the actual argument variables in the calling function.
However, the Call by Value method is less efficient when we need to pass large objects such as an array or a file to another function. Also, in some cases, we may need the actual arguments to be manipulated by another function. In such cases, the Call by Value mechanism is not useful. We have to explore the Call by Reference mechanism for that purpose. Refer the next chapter for a detailed explanation on the Call by Reference mechanism of C.
The Call by Reference approach involves passing the address of the variable holding the value of the actual argument. You can devise a calling method that is a mix of Call by Value and Call by Reference. In this case, some arguments are passed by value and others by reference.