Modular Programming and Function
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
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
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
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