You can view a function as a program within a program.
What happens inside of a function does not automatically affect what happens in the main program.
This includes names of variables.
Read the program below and predict what the output will be.
The output is:
5
10
This is to show you that the x and y variables in function "add" does not affect what it is #main.
If it did, you would get an output of:
5
5
This would happen IF what happens in the function did not affect what happens in #main since z is already defined as z = 2 + 3 and you are printing z in the last line of #main.
There is a way to make the variables inside a function affect #main but we will learn that later.
Returning Values to Main
You can send back a value from a function to where it was called out in main. "Called out" means where the function was activated.
Try the program below.
"return z" takes the value of the variable z inside of function "add" and sends that value to where add() was called out.
So in main, z = the value of variable z that is returned from function "add"
You can also directly return values. So at the end of function add() you could have wrote:
return x+y