1 / 15

Functions in C++

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

Download Presentation

Functions in C++

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Functions in C++ CSC 102 - M.M. Pickard

  2. Reasons for functions • Allow top-down design • Provide modularity • Allow reuse of code • Within the same program • From a library • Information hiding

  3. Structure charts • Show the relationships among modules • Show which modules call other modules • Do NOT show the internal logic of a module

  4. 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();

  5. 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 . . .

  6. 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.

  7. Using functions (other than main) in C++ • Declare the function • Define the function • Call the function

  8. 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

  9. Using functions in C++ : Definition • Declare the function • Define the function • Definition contains • function header • function body • Call the function

  10. 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.

  11. 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

  12. 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.

  13. 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.

  14. New programming concepts relating to functions in C++ • Parameters • actual • formal • Function declarations • Function definitions • Function calls (invocations)

  15. New software engineering concepts relating to functions • Modularity • Top-down design • Information Hiding • Reuse

More Related