Parameters
Languages vary in both the syntax and semantics of how thy pass parameters to functions.
<aside>
đź’ˇ formal parameters: defined in the function signature
actual parameters: arguments that’s passed in to the function when calling
</aside>
Parameter Passing Semantics describe how arguments are passed to functions.
- Pass by Value: aka pass by copy. Every argument is first evaluate, and a copy of the value is then passed to the function for local use
- Pass by Reference: secretly pass the address of each argument to the function. In the called function, all reads/writes to the parameter are directed to the data at the original address. Any change(including assignment) to a reference variable in a function will change the original variable
- Pass by Object Reference: all values/objects are passed by pointer to the called function. The called function can use the pointer to read/mutate the pointed-to-argument. Assignment would not work.
Aliasing
“Aliasing” occurs when two parameters to a function unknowingly refer to the same value/object and the function modifies it.
Parameter Passing
Positional: order of the arguments must match the order of the formal parameters
Named parameters: caller explicitly specifies the name of each formal parameter for each argument
Optional parameters: provide default values for specified formal parameters. (in language without mandatory argument labels, default parameters must all be placed at the end of the parameter list)
Variadic Functions
A function that caller can pass a variable number of arguments. Different language implement this feature differently.
Return Values
Bug vs. Errors vs. Results
- Bug: a flaw in a program’s logic - the only solution is aborting execution and fixing the bug
- Unrecoverable Errors: non-bug errors where recovery is impossible, and the program must shut down
- Recoverable Errors: non-bug errors where recovery is possible, and the program may continue executing