- 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 Reference
![]() Share with a Friend |
C Programming - C Function - Call by Reference
C Function - Call by Reference
In Call by Reference, instead of passing the value of a variable to a function, the address (reference) of the actual variable is passed. This allows the function to directly modify the actual data stored in the calling function. The original variables are modified, unlike in Call by Value, where only copies of the values are passed.
How Call by Reference Works
- Passing Address: In Call by Reference, the address (or reference) of the actual argument is passed to the function, not its value.
- Pointer Access: The function receives a pointer to the argument and accesses the original data via the pointer.
- Modification: Since the function works with the actual variable’s memory address, any modification made to the parameter inside the function will directly affect the original argument.
Syntax of Call by Reference
To implement Call by Reference, we use pointers. A pointer variable holds the memory address of a variable.
C
return_type function_name(data_type *parameter) {
// Function body where the parameter is accessed via the pointer
}
Example of Call by Reference
C
#include <stdio.h>
// Function Declaration using Call by Reference
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 with addresses of x and y
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;
// Dereferencing pointers to swap values
temp = *a;
*a = *b;
*b = temp;
// Values of x and y are modified directly
printf("Inside swap: a = %d, b = %d\n", *a, *b);
}
Explanation
- In main(), two integers x and y are defined with values 5 and 10, respectively.
- The swap() function is called with the addresses of x and y passed as arguments using the & operator.
- Inside the swap() function, we dereference the pointers (*a and *b) to access and modify the original values of x and y.
- Since we are working with the memory addresses, the changes made in the swap() function directly affect x and y in main().
Output:
Before swap: x = 5, y = 10
Inside swap: a = 10, b = 5
After swap: x = 10, y = 5
Explanation of Output:
- Inside the swap() function, the values of a and b are swapped using pointer dereferencing (*a and *b).
- As a result, the original values of x and y in main() are also swapped, since the function operates on their addresses (references).
Key Points of Call by Reference
- Modification of Actual Arguments: Changes made inside the function directly affect the original variables because the function works with their memory addresses.
- Efficiency: This method is efficient when large data structures (like arrays or structs) are passed to functions because it avoids copying large amounts of data.
- Pointers: A pointer is used to access the original variable in the function, and the argument is passed using the & (address-of) operator.
Advantages of Call by Reference
- Direct Modification: The function can directly modify the actual variables in the calling function.
- Efficient Memory Usage: It avoids copying large data structures or arrays, which saves memory and time, especially for large objects.
- Multiple Outputs: The function can return multiple values by modifying the arguments passed to it.
Disadvantages of Call by Reference
- Potential Side Effects: The function can unintentionally modify the original data, which might not be the desired behavior.
- Complexity: The use of pointers adds complexity to the code and can lead to issues like null pointer dereferencing if not handled properly.
When to Use Call by Reference
- When you want a function to modify the actual arguments.
- When passing large data structures like arrays, structs, or objects, Call by Reference is more efficient than Call by Value.
- When a function needs to return multiple values.
Summary of Call by Reference
- Call by Reference passes the address of the variable to the function, enabling it to directly modify the original variable.
- It is particularly useful when working with large data structures or when you need the function to modify the input arguments.
- The main drawback is that the original data can be unintentionally altered, so care must be taken to ensure that such modifications are intentional.
