Lecture7 Function Part1
Lecture7 Function Part1
At
the end of this lecture, you should be
able to
explain the importance of using function
define and call the function
pass data into a function
differentiate function that returns a
value and function that does not return
a value
2
MODULAR PROGRAMMING
Modular programming: breaking a program up into
smaller, manageable functions or modules
3
4
DEFINING AND CALLING FUNCTIONS
5
FUNCTION DEFINITION
Definition includes:
name: name of the function. Function names follow same
rules as variables
parameter list: variables containing values passed to the
function
body: statements that perform the function’s task, enclosed
in {}
return type: data type of the value that function returns to
the part of the program that called it
6
FUNCTION DEFINITION
7
FUNCTION HEADER
8
FUNCTION RETURN TYPE
10
CALLING A FUNCTION
11
FUNCTION PROTOTYPES
return type
number of parameters
12
FUNCTION PROTOTYPES
13
PROTOTYPE
15
EXAMPLE (CONTINUE)
16
SENDING DATA INTO A FUNCTION
Can pass values into a function at time of
call
c = sqrt(a*a + b*b);
Values passed to function are arguments
18
FUNCTION CALL
Value
of argument is copied into parameter
when the function is called
Function can have > 1 parameter
Theremust be a data type listed in the
prototype () and an argument declaration in
the function heading () for each parameter
Arguments will be promoted/demoted as
necessary to match parameters
19
CALLING FUNCTIONS WITH MULTIPLE ARGUMENTS
20
CALLING FUNCTIONS WITH
MULTIPLE ARGUMENTS ILLUSTRATION
21
PASSING DATA BY VALUE
24
RETURNING A VALUE FROM A FUNCTION
25
RETURNING A VALUE – THE RETURN
STATEMENT
26
EXAMPLE RETURNING A VALUE FROM A
FUNCTION
In a value-returning function, the return statement
can be used to return a value from the function to the
point of call. Example:
return type
28
6-29
30
LOCAL AND GLOBAL VARIABLES
local variable: defined within a function or block;
accessible only within the function or block
Other functions and blocks can define variables
with the same name.
When the function begins, its local variables and
its parameter variables are created in memory,
and when the function ends, the local variables
and parameter variables are destroyed.
This means that any value stored in a local
variable is lost between calls to the function in
which the variable is declared.
31
LOCAL AND GLOBAL VARIABLES
32
LOCAL VARIABLE LIFETIME
33
INITIALIZING LOCAL AND GLOBAL VARIABLES
34
GLOBAL VARIABLES – WHY USE SPARINGLY?
35
LOCAL AND GLOBAL VARIABLE NAMES
36
STATIC LOCAL VARIABLES
Local variables
Only exist while the function is executing
Are redefined each time function is called
Lose their contents when function terminates
37
GLOBAL VARIABLES AND GLOBAL CONSTANTS
A global variable is any variable defined outside all the
functions in a program.
The scope of a global variable is the portion of the
program from the variable definition to the end.
This means that a global variable can be accessed by
all functions that are defined after the global variable
is defined.
You should avoid using global variables because they
make programs difficult to debug.
Any global that you create should be global constants
Global constants defined for
values that do not change throughout
the program’s execution.
The constants are then used for those values throughout
the program.
6-41
DEFAULT ARGUMENTS
A default argument is an argument that is passed
automatically to a parameter if the argument is missing
on the function call.
(Program Continues)
DEFAULT ARGUMENTS