Answers ( 1 )

  1. 14 Mar 2025

    In programming, Call by Value and Call by Reference are two different ways of passing arguments to functions.

    1. Call by Value

    In Call by Value, a copy of the actual parameter is passed to the function.  Any changes made to the parameter inside the function do not affect the original value.

    Example in C programming:

    #include <stdio.h>
    void changeValue(int x) {
    x = 20;
    }

    int main() {
    int a = 10;
    printf("Before function call: %d\n", a);
    changeValue(a);
    printf("After function call: %d\n", a);
    return 0;
    }

    The function changeValue() modifies the value of x, but since x is a copy of a, the original a remains unchanged.

    2. Call by Reference

    In Call by Reference, the function receives a reference or address of the actual parameter.  Any changes made inside the function directly modify the original variable.

    Example

    #include <stdio.h>
    void changeValue(int *x) {
    *x = 20;
    }

    int main() {
    int a = 10;
    printf("Before function call: %d\n", a);
    changeValue(&a);
    printf("After function call: %d\n", a);
    return 0;
    }

    The function change Value() takes a pointer to x, which holds the address of a. Modifying *x changes the actual value of a, as the function operates directly on its memory location.

    0

Leave an Answer


lyciru

Ask by lyciru

 Prev question

Next question