CIS22A - Notes for Week 7, 11/4-11/7

Announcements and Reminders


Topics

Terminology

Introduction to Functions

What is a function?

Why write functions?

How to ...
  • Function definition
  • Function declaration (prototype)
  • Function call
  • variables inside functions
    • local variables
  • function arguments
  • function return values
Examples
  1. Print a simple message
  2. Print the sum of two numbers
  3. Return the sum of two numbers
  4. Return the sum of two random numbers
  5. Return the square root of the sum of two squares (recall the quadratic formula)
  6. Print a random card
  7. Print an assigned card
  8. Return the sum of 4 numbers
  9. Return the minimum of 11 numbers (think assignments)
  10. Return an the total of 11 assignments (after discarding the minimum)

Functions: Pass by value and return by value

Review of function concepts

  • function definition
    • function heading
    • body of function
  • function declaration or prototype
    • It is a good idea to write a function prototype.
    • It is required to write a function prototype if the function is defined after it is called.
    • A prototype's argument types and return types must match the function heading.
    • A prototype's argument names do not have to match the function arguments names.
    • A prototype's arguments must include argument types, but not argument names.
    • It's a good idea to use function argument names that identify their purpose.
  • function call
  • function arguments
    • pass by value
    • variables or values that are passed to functions by value are copied in stack memory.  It is the copy that is used inside the function.  The original variable in the calling function is unchanged by the function.
  • function return
    • return type
    • return value
    • return by value
    • How do you use the function return?
    • Do you have to use the function return?
      The answer is "no", but make sure you have a good reason not to.

Local and Global Variables

Local Static Variable


Function Design

How do you decide:
  • should the function have a void argument, one argument, or multiple arguments?
  • should the function have a void return, or should it return a value?
There may not be an exact right answer to the design questions, but often there is an easier, more obvious, or more direct approach.  These questions may help your decision:

What do you want the function to do?
This needs to be clear in your mind.

Do you have to provide data to the function?
If so, you will need (an) argument(s).

Do you have to provide more than one value to the function?
If so, you will need multiple arguments.

Is the function supposed to perform a calculation and print a value?
Maybe the function doesn't need to return a value.

Is the function supposed to perform a calculation and return a value?
If so, then you need a return.

Is the function supposed to print some output or return a value?
If it prints, maybe you don't need a return.  If it returns, maybe it doesn't need to print.
"Write a function that prints ..."
"Write a function that returns ..."

Where does the function get its values?
User input, random generation, no values needed, must get values externally.

Is the function too trivial?
Maybe you don't need it.
Is the function supposed to perform a calculation and return a value?

Is the function too complicated?
If so, "spin off" (an)other function(s).

Advice

  • Don't pass arguments that you don't need.
  • Don't return a value that is not meaningful.
  • Keep it as simply as possible.
  • Think small, think globally, think reuse.
  • Stay away from global variables.

In-class Practice Exercises

Write a prototype for each problem.  Choose a meaningful function name, appropriate function argument(s), and a return type.  Note, there is not necessarily an exact right answer, but there may be a best choice.

Write a function
  1. that prompts for and prints your name.
  2. that prompts the user for an even number and continues prompting until an even number is entered
  3. returns the cube root of a number
  4. prints the cube root of a number
  5. calculates the cube root of a number
  6. determines the average of 5 numbers
  7. returns the maximum of a some numbers in a file
  8. checks for the existence of a file
  9. converts inches to feet
  10. changes an unsigned int to an int
  11. changes an int to an unsigned int
  12. calculates the area of a triangle
  13. determines if a number less than 100 is prime or not

Videos

Functions

Creating Functions That Use Parameters

Functions That Use Multiple Parameters

Read Me
function

function definition

function declaration
prototype

argument

return value
return type

local variables

scope