5. Chapter 6 Functions - compscisi/Programming-Fundamentals-1-SI GitHub Wiki

Parts of a Function:

Alt text

Key Terms

  • Function: named collection of statements to perform a task
  • Function call: statement causes a function to execute
  • Function definition: statements that make up a function
  • Return type: data type returned by the function
    • If itโ€™s a datatype, it can be returned.
    • Use โ€œvoidโ€ to return nothing
  • Name: the name of the function. Follows same naming scheme as variables.
  • Parameters (parameter list): variables to be passed into the function
    • Parameters can be passed by value
    • This means a copy of the initial value is made, then the function works with the copy. The initial value remains untouched.
    • Parameters can also be passed by reference (denoted by โ€œ&โ€)
    • A reference variable is just like passing by value, except no copy is made. This means the original variable passed in is changed within the function.
  • Body: The actual function, the meat. Enclosed with { }.
  • To call a function, you include the name AND parameter list, even if that list is empty!
    • ex: toString();

Animation for Passing by Reference vs. Passing by Value

Alt text