Join Regular Classroom : Visit ClassroomTech

C Programming – codewindow.in

Related Topics

C Programing - Introduction

void increment(int x) {
    x++;
}

int main() {
    int num = 5;
    increment(num);
    printf("%d", num); // Output: 5
    return 0;
}
In this example, the "increment" function takes an argument "x" by value, and increments it by 1. However, when the function is called with the variable "num", a copy of its value is passed to the function, and any changes made to it within the function do not affect the original value of "num".
In call by reference, a reference or pointer to the memory location of the argument is passed to the function. The function can then access and modify the original value stored at that memory location. For example:
void increment(int *x) {
    (*x)++;
}

int main() {
    int num = 5;
    increment(&num);
    printf("%d", num); // Output: 6
    return 0;
}
In this example, the "increment" function takes an argument "x" by reference, which is represented by a pointer to an integer. When the function is called with the address of "num", it can access and modify the original value stored in that memory location by dereferencing the pointer and incrementing its value. The value of "num" is therefore changed to 6.

      

Go through our study material. Your Job is awaiting.

Recent Posts
Categories