Modular Programming 2/16/01 Lecture #5 16.070: Outline
Modular Programming 2/16/01 Lecture #5 16.070: Outline
070
Outline
! Need for, and benefits of, modular programs ! How to design modular programs ! Using functions to build modular programs
Fesq, 2/16/01
16.070
Fesq, 2/16/01
16.070
Modules are integrated to become a software system that satisfies the problem requirements
! To integrate successfully, original decision must be good and interfaces between modules must be correct
Fesq, 2/16/01
16.070
module 1 spec
design
module 1 design
build, test 1
module code 1
Final code
module 2 spec
design
module 2 design
build, test 2
module code 2
module 3 spec
design
module 3 design
build, test 3
module code 3
Fesq, 2/16/01
16.070
Fesq, 2/16/01
16.070
Fesq, 2/16/01
16.070
Fesq, 2/16/01
16.070
main_program
print_menu
move_robot
Fesq, 2/16/01
16.070
Fesq, 2/16/01
16.070
Portable: Individual modules can be modified to run on other platforms Re-usable: Modules can be re-used within a program and across programs
Fesq, 2/16/01
10
16.070
Fesq, 2/16/01
11
16.070
Function Elements
Function Definition - used to define/implement a function Function Calls - used to invoke/run a function
! Passing data between functions
Function Declarations/Prototypes - used to identify a function to the compiler prior to calling the function
Fesq, 2/16/01
12
16.070
Function name - main or unique user-defined (Optional) List of parameters enclosed in parentheses (void if none) Function body - variable declarations/statements to express algorithm, in brackets
Fesq, 2/16/01
13
16.070
Fesq, 2/16/01
14
16.070
Function Calls
To run a function, it must be called by another function To call a function ! List name of function When a function is called
! Program control passes to called function ! Code corresponding to called function is executed ! Function code is kept in separate area of main memory during execution ! After function body completes execution, program control returns to calling function
Fesq, 2/16/01
15
16.070
Parameter Passing
Arguments can be used to supply input to a function Values of actual parameters are copied to memory locations of corresponding formal parameters in function's data area
! Call function move_robot(user_input) ! Value of user_input copied into direction
Fesq, 2/16/01
16
16.070
int main(void) void move_robot(char direction) { { char user_input; /* move robot in direction */ user_input = print_menu(); /* specified by passed */ move_robot(user_input); /* argument */ } }
Fesq, 2/16/01
17
16.070
! Output from a function: return a value from a function ! Global variables, declared in source file outside/before function defs
Fesq, 2/16/01
18
16.070
Consists of
! Function type ! Function name ! List of function parameter types enclosed in parentheses ! Terminating semicolon e.g., void move_robot(char direction); e.g., char print_menu(void);
Fesq, 2/16/01
19
16.070
Scope of Functions
Function Declaration/Prototype defines region in program in which function may be used by other functions
! Global prototypes: Function prototype placed outside function definitions
Scope begins where prototype is placed and extends to end of source file Any function in program may use it Usually appear before definition of function main
Fesq, 2/16/01
20
16.070
Fesq, 2/16/01
21
16.070
Summary
Today we learned
! What modular programming is ! Why we want to design modular programs ! How to implement modular programs using functions
Readings:
! Review chapter C6 with this lecture ! For Tuesday (Monday classes), read C5.1-C5.8 on Variables and Operators. ! For Wednesday, read C4 ! For Friday, read C9
Note: Material for Weeks 3 and 4 have been swapped to provide you with more programming tools Errata: 16.070 TA office is 33-112
Fesq, 2/16/01
22
16.070