C Programming - Beginner to Brilliance / Functions in C

Call by Value vs Call by Reference

🧠 What is it?

When you call a function and pass values to it (called arguments), those values are received by the parameters of the function. How they’re received defines the calling method:

  • Call by Value 📦 – You send a copy of the value.

  • Call by Reference 📡 – You send the address (reference) of the value.


📦 Call by Value (Default in C)

✅ What happens?

  • A copy of the actual value is passed.

  • Any changes made inside the function do not affect the original variable.

  • The original variable and function parameter are completely separate.

🧠 Real-Life Analogy:

You photocopy your marksheet and send the copy to someone. If they write “Fail” on the copy, your original marksheet is unaffected.

💡 Example:

#include <stdio.h>

void changeValue(int x) {
    x = 50; // Changing the value
    printf("Inside function: x = %d\\n", x);
}

int main() {
    int a = 10;
    changeValue(a);
    printf("Outside function: a = %d\\n", a);
    return 0;
}
Inside function: x = 50
Outside function: a = 10

🟢 Explanation:

  • x was changed inside the function, but a remained unaffected.

  • This is because x was just a copy of a.


📡 Call by Reference (Using Pointers in C which we will study in further chapters)

🧠 C does not support true call-by-reference, but you can achieve the same using pointers.

✅ What happens?

  • The address of the variable is passed.

  • Any change made affects the original variable, since both refer to the same memory location.

🧠 Real-Life Analogy:

You give someone the original house key. If they repaint your wall, your actual home gets painted — not a copy!

💡 Example:

#include <stdio.h>

void changeValue(int *x) {
    *x = 50; // Changing the actual value using pointer
    printf("Inside function: x = %d\\n", *x);
}

int main() {
    int a = 10;
    changeValue(&a);  // Passing address of 'a'
    printf("Outside function: a = %d\\n", a);
    return 0;
}
Inside function: x = 50
Outside function: a = 50

🟢 Explanation:

  • x refers to the value stored at address of a.

  • Changing x changes a directly.


🔍 Difference Table: Call by Value vs Call by Reference

Feature

Call by Value 📦

Call by Reference 📡

Data Passed

Copy of the variable

Address (reference) of variable

Affects Original Value?

❌ No

✅ Yes

Memory Usage

More (copying data)

Less (just addresses)

Default in C?

✅ Yes

❌ No (need pointers)

Safe from side effects?

✅ Yes

❌ No (can change original)


🧪 Bonus: When to Use What?

Use Case

Method

You want to keep original data safe

Call by Value

You want to modify original data

Call by Reference (Pointers)

Working with large data (efficiency)

Call by Reference


⚠️ Common Mistakes to Avoid

  • 🚫 Don’t forget to use* to dereference(accessing the value stored at a memory address) in call-by-reference.

  • ⚠️ Forgetting to use & when passing address.

  • 🤔 Confusing value vs reference when debugging.


🧠 Example together:

#include <stdio.h>

void test(int x, int *y) {
    x = 100;
    *y = 200;
}

int main() {
    int a = 5, b = 10;
    test(a, &b);
    printf("a = %d, b = %d\\n", a, b);
    return 0;
}
a = 5, b = 200