Title: Chapter 7 - Functions
1Chapter 7 - Functions
2Functions
- Code group that performs single task
- Specification refers to what goes into and out of
function - Design refers to functions task(s)
- Two groups
- Library
- Programmer-defined
Lesson 7.1
3Function Categories
- Return no value
- Return a single value
- Use pass by reference
Lesson 7.1
4Function Basics
- Need three things to use function
- Function declaration (prototype)
- Indicates function exists and describes it
- Must be done before use of function
- Function call (invocation)
- Causes control to transfer from function to
function - Function definition
- Header line followed by function body enclosed in
braces
Lesson 7.1
5Function Declaration
- Indicates function name, return type and types of
values passed to function - General form
- ftype fname (atype, atype)
- ftype is function return type
- fname is function name
- atype is argument type (may be different)
- multiple argument separated by commas
Lesson 7.1
6Function Call
- Transfers program control to function
- General form
- fname (exp1, exp2)
- fname is function name
- exp1 and exp2 are arguments
- Variables or constants
- Called argument list
Lesson 7.1
7Function Definition
- Includes functions executable statements
- General form (example with two arguments)
-
- ftype fname (atype aname, atype aname)
-
-
-
-
Lesson 7.1
8Argument Lists
- Lists in function call and function header must
coordinate - number
- order
- type
- Example
kinetic_energy (mass, velocity) void
kinetic_energy (int m, double v)
Lesson 7.1
9Function Arrangement
- Not necessary for first function to be main
- Function definition must be located outside body
of any other function - Can write function declaration outside body of
all functions or within function - if within, can only be called from function where
declared - Function declaration MUST appear before call
Lesson 7.1
10Function Storage
- When function called, memory allocated for
- Variables in argument list
- Variables declared in function body
- When completes execution, memory freed and
variable values lost - Can prevent lost and maintain
- Called multiple times, allocated and freed
repeatedly
Lesson 7.1
11Common Errors
- Argument order not matching between function call
and header - Mismatching data types
- Pass more information than function needs to
complete task
Lesson 7.1
12Returning a Value
- Two items needed
- appropriate return type for function
- return statement in function
- Function declaration and definition list return
data type - int fact(double) int fact(double arg)
- return statement form
- return expression
Can put expression in ( ).
Lesson 7.2
13Return Statement
if (expression) return (a)
else return (b)
- Considered to be jump statement
- Can appear anywhere in function body
- More than one return statement can appear in
function
Lesson 7.2
14Returning Value From main
- void type function do not return values
- control transfers back to calling function
automatically - int main ( )
- return (0)
- Returns value 0 to operating system indicating
normal termination
Lesson 7.2
15Recap Pass By Value
- Default
- Function called
- Memory set aside and value copied into new memory
location - Calculations done inside function using value
- When function finished executing, memory location
destroyed
Lesson 7.2
16Pass By Reference
- Use argument list to allow function to directly
use calling function's values - Reference symbol required in function
declaration and header - Indicate arguments that will have values modified
- Create aliases for original variable names
Lesson 7.3
17Pass By Reference
void func_name (int, double, double,
int) func_name (x, y , z, a) void func_name
( int b, double c, double d, int e)
Lesson 7.3
18Scope
- Refers to region in which declaration is active
- Three kinds of scope
- Block
- Variable valid with block of code enclosed in
braces - Function
- Variable valid only within function where defined
- File
- Variable valid throughout all modules in file
- Determined by identifier's declaration location
Lesson 7.4
19Scope
n
j
Lesson 7.4
20Storage Classes
- Allows manual modification to scope and storage
rules - Stated in the declaration
- Four specifiers
- register store in register
- auto memory freed after function executes
- static memory persists after function executes
- extern global variables among files
Lesson 7.5
21static
- Variable maintains storage space and value after
function finishes executing - Memory reserved and initialized only once
- First time function called
- General form static type variable
- Can be initialized in declaration
- Done once, does not initialize on other calls
Lesson 7.5
22Comparing global and static Variables
- Similarity
- Permanent storage created for both
- Difference
- Scope
- Static variables accessed only from function in
which declared - Global variables accessed from any function
- With extern from any file
Lesson 7.5
23extern
- Multiple files help manage large programs
- Each compiled separately then linked to create
executable file - Share variable
- Variable declared as global without specifier in
one file - Other files extern type variable
Lesson 7.5
24Default Arguments
- Argument assigned particular value when argument
omitted in function call - Values specified in function declaration
- Must have ordinary argument listed prior to
default arguments
type function_name ( ordinary arguments,
default arguments)
Lesson 7.6
25Using Default Arguments
- Calling function
- Must be called with at least the number of
ordinary arguments
void commute (double, double 15.0, int 8)
commute ( 40.0 )
Cannot specify first and third anduse default
for second must haveall defaults last!
Call uses 40.0 for first argument, then
defaultvalues 15.0 for second argument and 8
for third
Lesson 7.6
26Function Overloading
- Defining two or more functions with same name
- Each function takes different number of arguments
- C knows which function is being called by
counting number of arguments used - Should not use default arguments since ambiguity
could result
Lesson 7.7
27Generating Random Numbers
- Need three functions
- rand ( )
- Returns pseudorandom integer in range 0 to 32767
- srand ( )
- Operates with rand( ) using global variable
invisible to programmer - time ( )
- Returns number of seconds from midnight
- Need mod () operator to restrict range
Lesson 7.8
28Generating a Random Number
- rand( ) and srand ( ) work together
- srand ( ) automatically "seeds" function rand ( )
- Functions linked through global variable
- time ( ) used to get true random effect
srand (time (0)) rand ( )
Call to createrandom number
Lesson 7.8
29Random Number in Specific Range
- Use mod operator
- Example n rand ( )
roll (n 6) 1
Simulate roll of die so result should be integer
from 1 to 6
Lesson 7.8
30Summary
Learned how to
- Define and call functions
- Determine the scope of a variable
- Pass values by reference
- Overload functions
- Create random numbers
Chapter 7