Local Variables
Inside a function, you can create variables, just like in main. But such a variable exists only inside its function: it is created when the function runs, and it disappears when the function ends. Outside the function, the compiler doesn't know it:
#include <stdio.h>
void print_total(void) {
int total = 5 + 3;
printf("%d\n", total);
}
int main(void) {
print_total();
printf("%d\n", total);
return 0;
}The program doesn't compile:
main.c:10:20: error: use of undeclared identifier 'total'
10 | printf("%d\n", total);
| ^~~~~
1 error generated.On line 10, main tries to print the variable total, which exists only inside the function print_total. Use of undeclared identifier means that the program uses a name that the compiler doesn't know in this place. Parameters are the same: they exist only inside their function.
A variable created inside a function is local: it belongs only to that function. The part of a program where a variable exists is called its scope. You already know that a variable created in a block, such as the i of a for loop, exists only up to the } of that block. The body of a function is a block too. And it applies to main as well, because main is a function: other functions can't see its variables. When a function needs a value from main, pass it as an argument.
Why is it good that local variables stay inside their functions?
- Functions don't get in each other's way. Two functions can each have a variable named
total, and those are two different variables: changing one doesn't change the other. - To use a function, you only need to know what to give it (the arguments) and what it gives back (the return value). How it works inside is its own business.
- Every call starts afresh: the variables of the function are created again, with nothing left over from the previous call.
A parameter is a local variable too. When a function is called, its parameter gets a copy of the argument. When the function changes its parameter, it changes only the copy, and the variable in main stays the same:
#include <stdio.h>
void reset_score(int score) {
score = 0;
}
int main(void) {
int score = 10;
reset_score(score);
printf("%d\n", score);
return 0;
}This program prints:
10The function set its parameter score to 0. But the variable score in main is a different variable that only has the same name, and it stayed 10.
So the rule is: values go into a function through its parameters and come out with return. When you want a function to give you a new value, return it, and store it where you call the function, for example score = add_bonus(score, 10);. In the next lesson, on pointers, you will learn another way: how a function can change a variable of the code that called it.
Instructions
The program doesn't compile: main prints bonus, but that is a parameter of the function add_bonus, so it exists only inside the function. In main, above the call, create a variable bonus with the value 10, and pass it to the function in place of the number 10. The program then prints Bonus: 10.
The score is still 50, though: the function added the bonus only to its copy. Fix it: the function returns score + bonus (so its return type is int), and main stores the result in its variable: score = add_bonus(score, bonus);. The program prints Score: 60.
Start programming for free
8/12