- C Programming Tutorial
- C - Home
- Basics of C
- C - Introduction
- C - Features
- C - Basics
- C - History
- C - Structure of C Program
- 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 - String Functions
- C - Static Keyword
- C - Random Number Generation
- C - Command Line Arguments
C Programming - C Function - Call by Value
![]() Share with a Friend |
C Programming - C Function - Call by Value
C Function - Call by Value
In C, Call by Value refers to the method of passing arguments to a function in which the actual parameter's value is passed to the function. The function works with a copy of the value, so any modifications made to the parameter inside the function do not affect the original argument.
How Call by Value Works
- Passing Parameters: In Call by Value, when a function is called, the value of the argument is copied into the formal parameter of the function.
- Local Copy: Inside the function, the formal parameter acts as a local variable, which holds a copy of the actual value passed.
- No Effect on Actual Argument: Changes made to the formal parameter inside the function do not affect the original argument because only a copy is modified.
Syntax of Call by Value
C
return_type function_name(parameter1, parameter2, ...) {
// Code that uses parameters
// Changes to parameters won't affect actual arguments
}
Example of Call by Value
C
#include <stdio.h>
// Function Declaration
void swap(int a, int b);
int main() {
int x = 5, y = 10;
printf("Before swap: x = %d, y = %d\n", x, y);
// Function Call
swap(x, y);
printf("After swap: x = %d, y = %d\n", x, y);
return 0;
}
// Function Definition
void swap(int a, int b) {
int temp;
temp = a;
a = b;
b = temp;
// This swap does not affect the original variables in main
printf("Inside swap: a = %d, b = %d\n", a, b);
}
Explanation
- In the main() function, two integers x and y are defined with values 5 and 10, respectively.
- The swap() function is called with x and y passed as arguments.
- Inside the swap() function, the values of a and b are swapped.
- However, since the swap() function uses Call by Value, the original values of x and y in main() remain unchanged.
Output:
Before swap: x = 5, y = 10
Inside swap: a = 10, b = 5
After swap: x = 5, y = 10
Explanation of Output:
- Inside the swap() function, a and b are swapped, but these changes only affect the local copies a and b. The original values x and y in main() are not changed.
- This is a clear example of Call by Value.
Key Points of Call by Value
- Modifications inside the function do not affect the actual arguments passed from the calling function.
- A copy of the actual parameter is passed to the function.
- Useful when you do not want to alter the original values in the calling function.
- The original variable's value remains unchanged outside the function.
Advantages of Call by Value
- Safety: The original arguments are not modified, providing safety from unintentional changes.
- Simplicity: It is easier to understand and implement as only a copy of the argument is passed.
Disadvantages of Call by Value
- Memory Overhead: A copy of the arguments is created, which can consume extra memory, especially for large data structures (like arrays or structs).
- Limited Modification: Since the actual values are not modified, any changes to the parameters inside the function do not affect the calling function.
When to Use Call by Value
- When you need to protect the original data from being modified.
- When the arguments are small in size and do not require modification. For larger data, a different method like Call by Reference is often more efficient.
