1. C Programming for Beginners: Writing Your First Hello World Program in C in 2023 2. Mastering Datatypes in C Programming: A Comprehensive Guide for Beginners in 2023 3. Complete Guide to % Format Specifiers in C for Beginners in 2023 4. A Comprehensive Guide to Operators in C Programming: Learn Everything About Operators in C in 2023 5. Master C Operator Precedence and Associativity in 2023: Definition, Examples, and Code 6. Learn Increment and Decrement Operators in C: A Beginners Guide with Examples - Codzify 7. Mastering Bitwise Operators in C: A Comprehensive Guide with Examples - Codzify 8. Ternary Operators in C Programming: A Beginners Guide (2023) 9. Switch Statements in C 2023: Complete Guide for Beginners 10. Learn Functions in C Programming - A Complete Tutorial for Beginners in 2023 11. Call by Value and Call by Reference in C Programming - A Complete Tutorial for Beginners in 2023 12. Storage Classes in C Programming: A Comprehensive Guide for 2023 13. Arrays in C Programming: A Comprehensive Guide for 2023 14. Basics of 2D Array in C Programming in 2023: Definition, Initialization, and Accessing Elements 15. Beginners Guide to Strings in C Programming with Codes and Examples 16. Strings Functions in C Language - strlen, strcpy,strcmp,strcat functions 17. A Comprehensive Guide to Structures in C Programming in 2023 18. Learn Unions in C Programming with Codes, Examples, and Explanations in 2023 19. Dynamic Memory Allocation in C: Understanding malloc, calloc, realloc, and free Functions 20. Introduction to C Programming 21. Hello world program in C 22. String functions in C - strncat, strcpy, strncpy, strchr, strrchr, strstr functions 23. Program for Fibonacci number in C - Codzify.com 24. What is palindrome number in C? 25. Write a C program to find a factorial of a given number 26. Write a C program to check whether a given number is prime or not 27. Write a C program to check whether a given number is an Armstrong number or not. 28. Write a C Program to transpose a matrix 29. C Programming Tutorial: Understanding Comments and Identifiers with Code Examples 30. Master String Handling Functions in C in 2023 - Codzify Topics

Call by Value and Call by Reference in C Programming - A Complete Tutorial for Beginners in 2023

Article by: Manish Methani

Last Updated: November 3, 2021 at 2:04pm IST
3 min 11 sec read

  1. Call by Value Call by value is a method of passing arguments to a function by copying the values of the arguments into the function's parameters. The function then works with the copied values, leaving the original values unchanged. Here's an example of a function that uses call by value:
#include <stdio.ht>

void changeValue(int x) {
    x = 10;
    printf("The value of x inside the function is %d", x);
}

int main() {
    int x = 5;
    printf("The value of x before the function call is %d", x);
    changeValue(x);
    printf("The value of x after the function call is %d", x);
    return 0;
}

Output:

The value of x before the function call is 5
The value of x inside the function is 10
The value of x after the function call is 5

In this example, the changeValue() function takes an argument x and sets its value to 10. However, since we're using call by value, the original value of x in the main() function remains unchanged.

   2. Call by Reference Call by reference is a method of passing arguments to a function by passing their memory addresses instead of their values. The function then works with the original values, and any changes made to the parameters affect the original variables. Here's an example of a function that uses call by reference:

#include <stdio.ht>

void changeValue(int *x) {
    *x = 10;
    printf("The value of x inside the function is %d", *x);
}

int main() {
    int x = 5;
    printf("The value of x before the function call is %d", x);
    changeValue(&x);
    printf("The value of x after the function call is %d", x);
    return 0;
}

Output:

The value of x before the function call is 5
The value of x inside the function is 10
The value of x after the function call is 10

In this example, the changeValue() function takes a pointer to an integer x and sets its value to 10 by dereferencing the pointer. Since we're using call by reference, the changes made to x inside the function also affect the original value of x in the main() function.

Conclusion: In summary, call by value and call by reference are two methods of passing arguments to a function in C programming. Call by value copies the values of the arguments into the function's parameters, while call by reference passes the memory addresses of the arguments instead. Understanding the difference between these two methods is crucial when working with functions in C programming.

 

Explore Tech Guide:

Codzify Logo

Terms and Conditions    Cookie Policy   Refund Policy   Adsense Disclaimer

Contact: teamcodzify@gmail.com