Course contentsShow
← C
Lesson 141 of 1,2192. Variables and TypesFree lesson

Function parameters as local scope

Function parameters as local scope

What you'll learn: Understand how variables passed into a function act as private, temporary workspace for that function only.

The "Personal Assistant" Analogy

Imagine you are a manager. You have a document with the number 100 written on it. You hire a temporary assistant (a function) to do some math. You don't give the assistant your original paper; instead, you hand them a photocopy.

The assistant takes that photocopy to their own private desk, scribbles on it, changes the number to 500, and finishes their task. When they leave for the day, their desk is cleared and their photocopy is shredded. Back at your manager's desk, your original document still says 100.

In C, function parameters work exactly like that photocopy. They are created the moment the function starts and are destroyed the moment it finishes.

Local to the Braces

When you define a function like void calculate(int age), that age variable is "local" to the function's curly braces {}. It only exists inside those walls. Even if you have another variable named age in your main function, the two are completely different people who just happen to share the same name.

C reserves a tiny spot in memory for the parameter when the function is called, fills it with the value you provided, and then wipes that memory clean as soon as the function reaches its closing brace.

Seeing it in Code

Notice how the changes inside the function don't "leak" back out to the rest of the program:

#include <stdio.h>

void addFive(int score) {
    // 'score' is a local copy. 
    // It only exists inside these curly braces.
    score = score + 5;
    printf("Inside function, score is: %d\n", score);
}

int main() {
    int myScore = 10;
    
    // We pass the value of myScore to the function
    addFive(myScore);
    
    // myScore remains 10 because the function only 
    // messed with its own local copy.
    printf("In main, myScore is still: %d\n", myScore);
    
    return 0;
}

Why this matters

This "isolation" is a feature, not a bug. It means you can write functions without worrying that they will accidentally change variables in other parts of your program. Each function is a sealed box, and its parameters are its own private tools.

Key takeaway

Function parameters are local variables that are born when the function is called and die when the function ends.