Menu
Coddy logo textTech

Pass-by-Value

Part of the Logic & Flow section of Coddy's C journey — lesson 25 of 63.

When you pass a variable to a function in C, the function receives a copy of that variable's value, not the variable itself. This fundamental concept is called pass-by-value.

Here's what happens: if you have a variable x with the value 10 and pass it to a function, the function gets its own separate copy of that value. Any changes the function makes to this copy don't affect the original variable back in the calling function.

void tryToChange(int num) {
    num = 99;  // This only changes the copy
}

int main() {
    int x = 10;
    tryToChange(x);
    // x is still 10, not 99
    return 0;
}

This behavior protects your original data from being accidentally modified by functions. However, it also means that if you want a function to actually change a variable's value, pass-by-value won't work. Understanding this limitation is crucial as you move forward to learn about pointers and how they can solve this problem.

challenge icon

Challenge

Easy

Write a C program that demonstrates the pass-by-value concept by attempting to modify a variable inside a function and observing that the original variable remains unchanged.

Your program should:

  1. Create a function named tryToModify that takes an integer parameter called number
  2. Inside the tryToModify function, add 50 to the number parameter
  3. Inside the tryToModify function, print the modified value in the format: Inside function: [value]
  4. In the main function, declare an integer variable named original and initialize it with the value 25
  5. Print the original value before calling the function in the format: Before function call: [value]
  6. Call the tryToModify function and pass the original variable as an argument
  7. After the function call, print the value of original again in the format: After function call: [value]

Your output should demonstrate that the original variable's value remains unchanged despite the modification inside the function:

Before function call: 25
Inside function: 75
After function call: 25

This challenge demonstrates the fundamental concept of pass-by-value in C, where functions receive copies of variables rather than the variables themselves. Any modifications made to the parameter inside the function do not affect the original variable in the calling function.

Cheat sheet

In C, functions receive a copy of variable values, not the variables themselves. This is called pass-by-value.

When you pass a variable to a function, any changes made inside the function only affect the copy, leaving the original variable unchanged:

void tryToChange(int num) {
    num = 99;  // This only changes the copy
}

int main() {
    int x = 10;
    tryToChange(x);
    // x is still 10, not 99
    return 0;
}

This behavior protects your original data from being accidentally modified by functions, but means pass-by-value won't work if you want a function to actually change a variable's value.

Try it yourself

#include <stdio.h>

// TODO: Write your tryToModify function here

int main() {
    // TODO: Write your code here
    // 1. Declare and initialize the original variable
    // 2. Print the value before function call
    // 3. Call the tryToModify function
    // 4. Print the value after function call
    
    return 0;
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Logic & Flow