chap05 - Chapter 5 Chapter Function Basics Key Concepts Key functions value parameters invocation and flow of control header file function prototyping

chap05 - Chapter 5 Chapter Function Basics Key Concepts Key...

This preview shows page 1 - 9 out of 27 pages.

Chapter 5 : Chapter 5 : Function Basics Function Basics
Image of page 1
Key Concepts: Key Concepts: functions value parameters invocation and flow of control header file function prototyping activation records define directives ( #define ) file inclusion ( #include ) conditional compilation pseudorandom numbers iostream functionality iomanip manipulators formatted output fstream class ifstream fstream class ofstream file manipulation stdlib library assert library translation unit casting
Image of page 2
Functions A function is like an assistant that goes off to perform a particular task and than returns with its solution. It is a executable unit. It must has a interface. Functions improve clarity and enable software reuse. A C/C++ program at least has one function.
Image of page 3
Functions Previous examples Programmer-defined functions main() ApiMain() Library-defined functions cin.get() string member functions size() RectangleShape member function Draw() SimpleWindow member function Open()
Image of page 4
Don Don t reinvent the wheel ! ! ! ! ! ! t reinvent the wheel ! ! ! ! ! ! …… There are lots of libraries out there. There are lots of libraries out there.
Image of page 5
Function Prototypes Before a function can appear in an invocation its interface must be specified Prototype or complete definition FunctionType FunctionType FunctionName FunctionName ( ( ParameterList ParameterList ); ); Type of value that the function returns Identifier name of function A description of the form the parameters (if any) are to take. (Formal Parameters) Prototypes are normally kept in library header files (*.h)
Image of page 6
Function Prototypes int Max(int a, int b); Examples: bool Open(); void Draw(void);
Image of page 7
Function implementation Complete definition: the implementation of function FunctionType FunctionType FunctionName ( ( ParameterList ) { //the function body goes here //the function body goes here . . . . . . . . . . . . } int Max(int a, int b) { return (a > b) ? a : b; } Always begin with “{“ and end with “}”. Complete definitions are normally kept in implementation files (*.cpp).
Image of page 8
Image of page 9

You've reached the end of your free preview.

Want to read all 27 pages?

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

Stuck? We have tutors online 24/7 who can help you get unstuck.
A+ icon
Ask Expert Tutors You can ask You can ask You can ask (will expire )
Answers in as fast as 15 minutes