Introduction to Functions
What is a function?
Why write functions?
How to ...
- Function declaration (prototype)
- variables inside functions
Examples
- Print a simple message
- Print the sum of two numbers
- Return the sum of two numbers
- Return the sum of two random numbers
- Return the square root of the sum of two squares
(recall the quadratic formula)
- Print a random card
- Print an assigned card
- Return the sum of 4 numbers
- Return the minimum of 11 numbers (think assignments)
- 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
- that prompts for and prints your name.
- that prompts the user for an even number and
continues prompting until an even number is entered
- returns the cube root of a number
- prints the cube root of a number
- calculates the cube root of a number
- determines the average of 5 numbers
- returns the maximum of a some numbers in a file
- checks for the existence of a file
- converts inches to feet
- changes an unsigned int to an int
- changes an int to an unsigned int
- calculates the area of a triangle
- determines if a number less than 100 is prime or not
Videos
Read Me |
function
function
definition
function
declaration
prototype
argument
return
value
return
type
local
variables
scope |