Passing of variables to functions depends on the signature of the function, the size of the variable, the compiler, the platform and the optimization settings.
Passing of Variables
- By the language syntax, variables can be passed to functions:
- By value (or copy)
- By reference (passing a link to the original variable)
- By constant reference (same as reference, but the function can't
modify the variable)
- By pointer (passing the address of a variable), including all the
const
variations.
This is a high level view. In this view, we don't know if the variable is stored "in" memory or not. We don't know if the variable resides in registers. All we know is that we want to pass information to a function.
Implementation
A compiler has a lot of freedom when implementing the passing of variables.
Many compilers may pass values in registers, depending on how many registers are available, the size of the variable and the quantity of variables. In this case, the variable doesn't live in memory.
The compiler may not even "pass" the variable. The compiler may "inline" a function. In this case, the compiler continues emitting code without changing registers.
Some compilers use a stack-based memory area for passing variables. This is convenient because the compiler "pushes" the variables before calling the function and "pops" values after the function executes. Pushing and popping usually involve adjusting the value in a dedicated register; thus and easy and optimal technique.
The compiler may decide not to pass the variable, but pass a pointer to the variable. For example, when passing by reference, the compiler may decide to pass a pointer to the variable (address of the variable). In this case, the contents of the variable are not copied, but accessed directly. For example, if the variable (data) is in read only memory, the compiler may decide to pass a pointer to the data rather than waste time copying the data.
Variable Lifetimes
Variables that are passed by copy have a life time for the duration of execution within the function.
Variables that are passed by reference last until they are destroyed by the caller.
There are forms of variables that can have a longer lifetime. This set includes, but is not limited to, global variables, static variables, and variables allocated from dynamic memory (a.k.a. the heap).
Optimizations
There is a possibility that the compiler will not pass variables. For example, if the function doesn't use a parameter, the compiler may not pass one.
Compilers have been known to eliminate empty functions as an optimization. Thus no variables will be passed.
As stated earlier, when functions are "inlined", the calling overhead (including variable passing) is reduced or eliminated.
As an optimization, the compiler may remove variables or not call functions. For example, if calling a function results in intializing a variable, the compiler may prefer to perform an assignment instead.
So there are a lot of "it depends" for your question.
Hope this broadens your understanding of variable passing.