W6 - FUNTIONS Flashcards
W6-1: What is MODULAR DESIGN?
_ Procedural programming involves SEPARATING source code into SELF-CONTAINED components that can be accessed multiple times from different locations in a COMPLETE program.
_ This approach enables SEPARATE coding of each component and assembly of various components into a complete program.
W6-2: How MODULAR DESIGN works?
_ Modular design identifies the COMPONENTS of a programming project that can be developed SEPARATELY
_ Each module consists of a set of LOGICAL constructs that are related to one another.
_ A module may refer to other modules.
W6-3: DESIGN PRINCIPLES:
Some general GUIDELINES for defining a module include: _ the module is easy to UPGRADE _ the module contains a READABLE amount of code _ the module may be used as part of the solution to some other problem
For a STRUCTURED design: _ each module has one ENTRY point and one EXIT point _ each module is highly COHESIVE _ each module exhibits LOW coupling
W6-4: What is COHESION?
COHESION describes the focus: a highly COHESIVE module performs a SINGLE task and only that task.
In creating a cohesive module, we ask whether our tasks belong to that module:
_ a reason to INCLUDE a task is its relation to the other tasks within the module.
_ A reason to EXCLUDE a task is its independence from other tasks within the module.
W6-5: What is COUPLING?
_ COUPLING describes the degree of interrelatedness of a module w/t other modules.
_ The less INFO that passes btw the module and the other modules the better the design.
_ We prefer designs in which each module completely control its OWN computations and avoids transferring control data to any other module.
W6-6: Definition of CALLS, RETURNS, CALLER
_ (C) supports modular design through FUNCTION syntax
_ Functions transfer CONTROL btw one another.
_ When a function TRANSFERS control to another function, => it CALLS the other function.
_ Once the other function COMPLETES its task and transfers control to the CALLER function
=> that other function RETURNS control to its CALLER
W6-7: Definition of FUNCTION
_ A function consists of a HEADER and a BODY.
_ The BODY is the code block that contains the DETAILED instructions to be performed by the function.
_ The HEADER immediately precedes the body and includes the following IN ORDER
1/ the type of the function’s return value
2/ the function’s identifier
3/ a parentheses-enclosed list of parameters that receive data from the caller
W6-8: void Functions
_ A function that does not have to return any value has NO return type.
_ declare its return type as VOID and EXCLUDE any expression from the return statement.
W6-9: No Parameters
_ A function that does not have to receive any data does not require parameters.
_ insert the keyword VOID btw the parentheses.
W6-10: main
_ The main() function is a FUNCTION itself.
_ It is the function to which OS transfers CONTROL after loading the program into RAM.
_ main() returns a value of int type to OS once it has completed execution. _ A value of 0 indicates success to OS
W6-11: What is a FUNCTION CALL?
_ A FUNCTION CALL transfers control from the CALLER to function being called.
_ Once the function being called has executed its instructions, it returns control to the CALLER.
_ Execution continues at the point immediately following the CALL statement.
W6-12: Pass By Value
_ the mechanism of allocating SEPARATE memory locations for parameters and using the arguments in the FUNCTION CALL to initialize these parameters: pass by value.
_ Pass by value facilitates modular design by localizing consequences.