Week 4
Return Exam
Introduction to functions
examples of built in functions
  • sqrt & other math functions
void - verb names
non-void - noun names  - must return a value
function signature (aka. declaration/definition) vs function call
arguments and parameters
  • parameter - part of the function's signature
  • argument - part of the function call
non-void functions can only return one value but can have multiple return statements
//function aka  method, routine, sub-routine, sub
Why use functions?
  1. documentation
  2. modularization aka componentization  aka hiding the mess
  3. reuse - saves time and is more reliable
  1. reduces complexity: Functions can simplify a program greatly and make it easier to understand what is happening, and program tend to be shorter
Assign Homework
Read Chapter 5 sections 1-10
Prime Factors C++ (Due Tuesday)
c++ return statement
by value vs. by reference
By Value:
  • By default, arguments are passed by value to a function which means a copy is made of the argument
  • Any changes made to a parameter are only changing the copy, not the actual argument
  • It's like handing someone a photocopy of a paper you wrote; any changes the person makes to the photocopy doesn't change the original paper
By Reference:
  • This means the parameter is just another name for the argument
  • Therefore any changes made to the reference parameter will also change the argument (modifying x will change a, and modifying y will change b)
  • It's like handing someone the original paper you wrote; any changes the person makes to the original will affect the original.
  • Uses an ampersand (&)
only variables can be passed to a function that receives arguments by reference (literals cannot be passed by ref)
Work examples from Schaum's book chapter 5
Assign Homework
Work problems 5.21, 5.22, 5.24 and 5.25 from Schaum's book.  Use Boolean return values rather than integers for 5.21 and 5.22. (Due Wednesday)
Black Box Diagram -   From the diagram you can only see what is input and what is output but not how it works inside.
order of functions - functions must be "declared" before they are used
function prototypes - "prophecy" that you will write a function later
  •  allow you to organize function in any order you want
  •  if you do not write the function you will get a "linker" error
Declaration vs Definition
  • declaration - introduces an identifier and describes it.  This is what the compiler needs to know.
  • definition - instantiates/implements the identifier.  This is what the linker needs to know.
Assign Homework
Fraction Calculator Lab (Due Thursday)
 Function Lab day
switch-case statement - can only be with whole numbers
break vs. falling through
default case
if-else-if vs. switch-case
why switch is "unstructured"
goto - spaghetti code, goto statement considered harmful
start soft drink survey in class
Assign Lab/ Project
Soft drink survey (Due Monday)
12 Days of Christmas (Due Monday)
Checks project (Due Thursday)