Professional Documents
Culture Documents
PROGRAMMING
LECTURE 6 – PROCEDURAL PROGRAMMING
TOPICS
Functions
Function definition
Function Call
Argument and return
Passing argument
/* function 'swap'
* - does not return any value
* - takes 2 pointer variables*/
void swap(int *, int *)
/* function add
* - returns float value
* - takes 1 float variable and 1 integer variable*/
float add(float x, int y)
FUNCTION DEFINITION
Local variables are declared inside a function, they are local to that function
o It can be accessed only within that function
o Memory for it is allocated only when the function is invoked
o Memory for it is de-allocated when the control moves out of the function
Global variables are declared outside of the functions, they are common to all
functions
o It can be accessed by all functions in the program
o Memory for it is allocated when “program” gets executed
o Memory is de-allocated only at the end of the “program” execution
EXAMPLE: LOCAL/GLOBAL VARIABLES
RETURN STATEMENT
The values of the actual arguments are copied into the respective
formal arguments
Actual and formal arguments
o Refer to the different memory locations and
o Value of actual argument is copied into the formal argument
So, any changes made to the formal argument are not reflected in their
corresponding actual arguments
CALL/PASS BY VALUE EXAMPLE
ACTIVITY: DESIGN FUNCTIONS FOR A MENU
BASED PROBLEM
Given a menu based program with 4 options
Draw a Product Breakdown Structure
Identify modules (functions) with inputs / outputs
Implement modules
Implement main function