LEC 7 Functions
LEC 7 Functions
LEC 7 Functions
main
Level 3 Level 3
1
Programmer-Defined Functions
Modularize with building blocks of programs
Divide and Conquer
Construct a program from smaller pieces or components
Place smaller pieces into functions
Pieces are more manageable than one big program
Makes other functions smaller
Pieces can be independently implemented and tested
2
Programmer-Defined Functions
Readability
Function name should indicate operations performed
Reuse
Functions may be used multiple times in same program
Functions may be used in other programs
3
Components of Function Use
Three steps to implementing functions
1. Function declaration/prototype
If not defined before use
2. Function definition
3. Function call
Either prototype or definition must come first
4
Program Function Definition Structure
main first (preferred) main
Top down design
Level 2 Level 2 Level 2
Some prototypes required
Complete prototyping allows Level 3 Level 3
function definition in any
order
Level 3 Level 3
main is last – lowest level
Level 2 Level 2 Level 2
functions first
Bottom up design main
Prototypes not required
Example:
6
Function Declaration/Prototype
Placed before any calls
Generally above all functions in global space
May be placed in declaration space of calling function
Example
7
Alternative Function Declaration
Function declaration is 'information' for compiler, so
Compiler only needs to know:
Return type
Function name
Parameter list
Formal parameter names not needed but help readability
Example
8
2. Function Definition
Actual implementation/code for what function does
Just like implementing function main()
General format – header & basic block:
<return-type> fn-name (parameter-list) header
basic block
Example:
9
Return Statements
Syntax: return return-value-expression
Two actions
Sets return value
Transfers control back to 'calling' function
Good programming & course requirement:
One return per function
Return is last statement
10
3. Function Call
Using function name transfers control to function
1. Values are passed through parameters
2. Statements within function are executed
3. Control continues after the call
For value-returning functions, either
Store the value for later use
11
Parameters (Arguments)
Formal parameters/arguments
In function declaration
In function definition's header
'Placeholders' for data sent in
'Variable name' used to refer to data in definition of
function
Actual parameters/arguments
In function call
12
Parameter vs. Argument
Names used interchangeably
Technically parameter is 'formal' piece
while argument is 'actual' piece
Parameter! Argument!
13
Functions Calling Functions
We're already doing this!
main() IS a function calling printf!
Only requirement:
Function's declaration or definition must appear first
Common for functions to call many other functions
Function can call itself Recursion
14
Declaring Void Functions
Similar to functions returning a value
Return type specified as 'void'
Example prototype:
Return-type is 'void'
15
Declaring Void Functions
Nothing is returned
Void functions cannot have return statement with an
expression
Will return at end of function
Non-void functions must have return statement with an
expression
Example definition:
16
Calling Void Functions
From some other function, like main():
17
Function documentation
Used to aid in program maintenance
Comments at non-main definition header
Purpose of function
Parameters
Return
Class standard example:
18
main(): ‘Special’
Recall: main() IS a function
'Special'
It is the first function executed
Called by operating system or run-time system
Can return value to operating system
Value can be tested in command scripts
19
Scope of Identifier Names
Region of a program where identifier is visible
Begins at definition within block
Ends at end of block
Local variables
Name given to variables defined within function block
Can have different local variables with same name
declared in different functions
Cannot have duplicate local names within a function
20
Scope Rules
Local variables preferred
Maintain individual control over data
Need to know basis (Hidden)
Functions should declare whatever local data
needed to 'do their job'
21
Global Scope
Names declared 'outside' function bodies
Global to all functions in that file
Global declarations typical for constants:
Declare globally so all functions have scope, can use
22
Global Constants and Global Variables
Global variables?
Possible, but SELDOM-USED
Better alternative is to use parameters
Dangerous: no control over usage!
We do not use global variables in this class!
23
Block Scope
Declare data inside nested blocks
Has 'block-scope'
Note: All function definitions are blocks!
24
Lifetime
How long does it last
Allocation Deallocation
Normally variables are allocated when defined
Normally variables are deallocated at the end of block
25
Static & Lifetime
Variable definition modifier keyword: static
Static variables are only allocated once
Static variables are not deallocated until program ends
26
Programming in C
THE END
27