## Section4.9Calling Functions

Calling a function is also an expression in C. Review our introducing example, especially the call of fac in main:

int fac(int n) {
int res = 1;
/* ... */
return res;
}

int main(int argc, char** argv) {
/* ... */
int n = /* ... */;
int r = factorial(n);
/* ... */
}


How exactly are parameters passed to factorial? When a function is called, a container for each parameter with the appropriate size is allocated. Practically, the parameters are additional local variables of the called function. Their scope extends over the entire function and their life time ends when the function is left. Before execution continuous with the first statement of the function, all (comma-separated) arguments are R-evaluated. The resulting values are written to the corresponding containers. The parameters' containers are deallocated when the function returns. This approach is called call by value, since the values of the arguments are passed to the called function. Other programming languages (e.g. Pascal or C#) additionally allow to call by reference, that is to pass the containers' addresses to the function. Arguments that are passed by reference need to be L-evaluable. In C, we can simulate passing arguments by reference if we declare the function to take a parameter of pointer type (see Section 4.10).

A function terminates when a return-statement is executed. If the function has a void return type, we can omit the return-statment and the execution will return to the caller after the last statement of the called function's body. If the program has a different return type, it needs to be ended with return E;, where E is an expression with the appropriate type.