PROGRAMMING FOR COMPUTING
Modular Programming and
Function
Lecture Week # 5
Monday, 27th July 2015
CSC 1164
Topics to be covered
Element of Modular Programs
Structure of Modular Programming
Function that returns values
- Return statement
- Scope of Variables
- Duration of Variables
Function with Parameters
In-line function
Function That Return Values
If the function we are designing is expected to
produce and return only one value, we can use the
function name for this purpose;
The function name must have a return type other
than void
The return type is any data type, such as int,
double or char
Function That Return Values
int user_request ( )
Has int as its return type
Function is designed such that it will return an
interger value as its output under its name,
user_request
The return Statement
return ;
A function designed to return a value under its
name must contain at least one return statement
in its body.
If the return type of a function definition is
missing, the default is int.
If a function is not intended to return a value
under its name, its return type must be void.
Scope of Variables
Region in which it can used legitimately.
Function that require data as input to carry out its
tasks.
~ Function that calls another function may
communicate data to it and may receive data
from it.
Where in a program the declared variables can
be used legally to access their values.
Concepts of scope, visibility and duration of
variables.
Types of Variables
Depending on where they are declared;
1. Global variables
2. Local variables
3. Function formal parameters
Types of Variables
Depending on where they are declared;
1. Global variables
A variable declared outside any function in a source
file
Begins at the point of its declaration and terminates
at the end of the file
Are said to have file scope
Types of Variables
Depending on where they are declared;
2. Local variables
A variable declared within a block
Starts at the point of its declaration and extends until
at the end of the block
Also called as block scope
Multiple Declarations of Variables
and Visibility
Another consideration closely related to scope is
visibility of a variable;
Programs ability to access thats variables memory
location.
Accessing Hidden Global Variables:
The Scope Resolution Operator
It may be necessary to access hidden global
variables by using the scope resolution operator, ::
on the hidden global variable;
cout << \na = << a;
is to the local variable a declared in line 11.
Accessing Hidden Global Variables:
The Scope Resolution Operator
However, the statement in line 15,
cout << \nGlobal a = << ::a;
Will print Global = ??
Duration of Variables
At run time, a memory location is reserved for each
declared variable.
Depending on how declarations are made, such
memory locations exist for a certain period.
the time during which a memory location exists for
that variable at run time.
Duration of Variables
Types of duration:
1. Static
2. Local
3. Dynamic
Duration of Variables
Types of duration:
1. Static
Exist in the memory in fixed data segments until the
termination of the program.
Global variables are static variables; unless they have been
explicitly assigned values.
Variable with static duration are initialized to zero.
Duration of Variables
Types of duration:
2. Local
Physically created in the memory when the object code
corresponding to the enclosing block or function is
encountered during execution.
Exists until the end of that block or function.
Must be explicitly initialized.
Duration of Variables
Types of duration:
3. Dynamic
Variables created and destroyed by using specific operators at
run time
Have dynamic duration
main_program
reque
st
user_request
print_menu
draw_rectangle
draw_triangle