Call by Value & Call by Reference

Call by Value & Call by Reference

In C, function parameter passing can be broadly categorized into two modes: "Call by Value" and "Call by Reference." These two modes determine how the values of arguments are passed to functions.

Call by Value: In "Call by Value," a copy of the actual argument's value is passed to the function. This means that changes made to the parameter inside the function do not affect the original variable outside the function. This mode is often used for simple data types like integers and floats.

Here's an example illustrating "Call by Value":

#include <stdio.h>

// Function to increment a number by 1 (Call by Value)
void incrementByValue(int num) {
    num += 1;
}

int main() {
    int x = 5;
    printf("Before function call: x = %d\n", x);
    
    incrementByValue(x);
    
    printf("After function call: x = %d\n", x);
    
    return 0;
}        

Output:

Before function call: x = 5
After function call: x = 5        

In this example, x remains unchanged after calling the incrementByValue function because it operates on a copy of x.

Call by Reference (using Pointers): In "Call by Reference," a reference to the memory location of the actual argument is passed to the function. This means that changes made to the parameter inside the function will affect the original variable outside the function. This mode is often used when you want to modify the original variable.

Here's an example illustrating "Call by Reference" using pointers:

#include <stdio.h>

// Function to increment a number by 1 (Call by Reference)
void incrementByReference(int *num) {
    (*num) += 1;
}

int main() {
    int x = 5;
    printf("Before function call: x = %d\n", x);
    
    incrementByReference(&x); // Pass a reference to x
    
    printf("After function call: x = %d\n", x);
    
    return 0;
}        

Output:

Before function call: x = 5
After function call: x = 6        

In this example, x is modified inside the incrementByReference function because we passed a pointer to x, allowing the function to access and modify its value.

To summarize:

  • "Call by Value" passes a copy of the argument's value, and changes inside the function do not affect the original variable.
  • "Call by Reference" passes a reference to the argument's memory location, allowing changes inside the function to affect the original variable. This is typically achieved using pointers.

要查看或添加评论,请登录

Uttam Basu的更多文章

  • TypeCasting in C / C++

    TypeCasting in C / C++

    In C programming, type casting refers to the process of converting a variable from one data type to another. There are…

  • "Bit-banding" in ARM Cortex-M microcontrollers

    "Bit-banding" in ARM Cortex-M microcontrollers

    Bit-banding is a unique and powerful feature available in ARM Cortex-M microcontrollers, designed to simplify and…

    2 条评论
  • Include a header file multiple times in a source code file, What will be?

    Include a header file multiple times in a source code file, What will be?

    If you include a header file multiple times in a source code file (such as a C or C++ file), the compiler will process…

    1 条评论
  • BLE System Architecture

    BLE System Architecture

    Bluetooth Low Energy (BLE) stands as a wireless Personal Area Network (PAN) technology meticulously crafted and…

  • Diamond problem in C++

    Diamond problem in C++

    The "diamond problem" is a term used in object-oriented programming, particularly in languages like C++ that support…

    2 条评论
  • Volatile keyword in Embedded C

    Volatile keyword in Embedded C

    In embedded C programming, the "volatile" keyword is used to inform the compiler that a particular variable can change…

  • What is HDR mode in Camera?

    What is HDR mode in Camera?

    HDR stands for High Dynamic Range in the context of photography and camera technology. It is a technique used to…

  • Data sharing between Threads

    Data sharing between Threads

    Threads can share data with each other in a multi-threaded program through various mechanisms and synchronization…

  • Makefile

    Makefile

    A Makefile is a special file used in software development, particularly in C and C++ programming, to automate and…

  • Static and Dynamic Memory Allocation in C

    Static and Dynamic Memory Allocation in C

    Static and dynamic memory allocation are two distinct approaches to managing memory in C programming. Here's a detailed…

    3 条评论

社区洞察

其他会员也浏览了