You are on page 1of 18

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 .Duration of Variables • Function with Parameters • In-line function .Scope of Variables .

double or char .Function That Return Values If the function we are designing is expected to produce and return only one value.  The function name must have a return type other than void  The return type is any data type. such as int. we can use the function name for this purpose.

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 .

• If the return type of a function definition is missing. . the default is int.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 a function is not intended to return a value under its name. its return type must be void.

visibility and duration of variables.  Concepts of scope.Scope of Variables • Region in which it can used legitimately. • Where in a program the declared variables can be used legally to access their values. ~ Function that calls another function may communicate data to it and may receive data from it. . • Function that require data as input to carry out its tasks.

1. Global variables 2.Types of Variables Depending on where they are declared. Function formal parameters . Local variables 3.

Types of Variables Depending on where they are declared. 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 . 1.

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 .Types of Variables Depending on where they are declared.

Multiple Declarations of Variables and Visibility Another consideration closely related to scope is visibility of a variable.  Program’s ability to access that’s variable’s 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. . …is to the local variable a declared in line 11. :: on the hidden global variable. cout << “\na = “ << a.

the statement in line 15.Accessing Hidden Global Variables: The Scope Resolution Operator However. cout << “\nGlobal a = “ << ::a. Will print Global = ?? .

such memory locations exist for a certain period. a memory location is reserved for each declared variable. …the time during which a memory location exists for that variable at run time. .Duration of Variables • At run time. • Depending on how declarations are made.

Duration of Variables Types of duration: 1. Static 2. Local 3. Dynamic .

unless they have been explicitly assigned values.Duration of Variables Types of duration: 1.  Global variables are static variables. Static  Exist in the memory in fixed data segments until the termination of the program.  Variable with static duration are initialized to zero. .

 Must be explicitly initialized.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.

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 .