220 likes | 437 Views
Functions in C++. CSC 102 - M.M. Pickard. Reasons for functions . Allow top-down design Provide modularity Allow reuse of code Within the same program From a library Information hiding. Structure charts. Show the relationships among modules Show which modules call other modules
E N D
Functions in C++ CSC 102 - M.M. Pickard
Reasons for functions • Allow top-down design • Provide modularity • Allow reuse of code • Within the same program • From a library • Information hiding
Structure charts • Show the relationships among modules • Show which modules call other modules • Do NOT show the internal logic of a module
Function Flow of Control • The main function is invoked by the system when the program begins execution. • Each function call returns to the place that called it function2 main function1 function2(); function1();
C++ Program • Must contain a main function • Execution starts and ends with the main function • Main may call other functions, which may call others, and so on . . .
Function-to-function interface • One “calls” another • Data may be passed between the functions • “Parameters” • Passed by value or by reference • Value: A copy of the data is passed. • Reference: The address of the data is passed.
Using functions (other than main) in C++ • Declare the function • Define the function • Call the function
Using functions (other than main) in C++ : Declaration • Declare the function • use a prototype declaration • appears before the main • shows the return type, the function name, and the formal parameter list (the input parameters), e.g., int multiply (int num1, int num2); • Define the function • Call the function
Using functions in C++ : Definition • Declare the function • Define the function • Definition contains • function header • function body • Call the function
Using functions in C++ : Definition • Define the function • Definition contains • function header • return type • function name • formal parameter list (no semicolon) • function body • The function definition tells what the function does when it is called.
Using functions in C++: Calling • Declare the function • Define the function • Call the function • To call a function, use its name and supply values for the input parameters. • That is, a function call consists of the function name followed by the actual parameter list
Using functions in C++: Calling • Call the function • To call a function, use its name and supply values for the input parameters. • That is, a function call consists of the function name followed by the actual parameter list • The actual parameters may be constants, or they may be variables or expressions whose value can be determined at the time of the call.
Using functions in C++: Calling • Call the function • A function may return a single value (which must be the same type as the return type). • A function may have side effects • that is, it may perform some other action besides returning a single value.
New programming concepts relating to functions in C++ • Parameters • actual • formal • Function declarations • Function definitions • Function calls (invocations)
New software engineering concepts relating to functions • Modularity • Top-down design • Information Hiding • Reuse