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
EasyWrite 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:
- Create a function named
tryToModifythat takes an integer parameter callednumber - Inside the
tryToModifyfunction, add 50 to thenumberparameter - Inside the
tryToModifyfunction, print the modified value in the format:Inside function: [value] - In the
mainfunction, declare an integer variable namedoriginaland initialize it with the value 25 - Print the original value before calling the function in the format:
Before function call: [value] - Call the
tryToModifyfunction and pass theoriginalvariable as an argument - After the function call, print the value of
originalagain 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: 25This 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;
}This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Logic & Flow
1Pointers Fundamentals
What is a Pointer?Declaring PointersThe Address-Of Operator (&)The Dereference Operator (*)NULL PointersRecap: Pointer Basics4Project: Simple Text Utility
Project OverviewCounting Characters2Pointers and Arrays
Array Names as PointersArray Elements - PointersPointer ArithmeticComparing PointersRecap: Pointer Array Traversal5Pointers and Functions
Pass-by-ValuePassing Pointers to FunctionsModifying Vars via PointersA Classic Example: SwapPassing Arrays to FunctionsRecap: Function Pointer Args8Structs and Pointers
Pointers to StructsThe Arrow Operator (->)Passing Structs by ValuePassing Struct PointersDynamic Allocation of StructsRecap: Modifying Struct - Ptr11Final Recap Challenges
Recap: Dynamic String ConcatRecap: Array of StructsRecap: Word Frequency Counter3Character Arrays and Strings
Strings as char ArraysThe Null TerminatorString Input with scanfUsing strlen()Using strcpy()Using strcat()Using strcmp()Recap: Basic String Functions