{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

lecture10 Functions

# lecture10 Functions - CMPT 128 Introduction to Computing...

This preview shows pages 1–10. Sign up to view the full content.

1 © Janice Regan, CMPT 128, Jan 2007 CMPT 128: Introduction to Computing Science for Engineering Students Functions (1)

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
© Janice Regan, CMPT 128, Jan 2007 2 Modularization When solving a real engineering (or other) programming problem it will be necessary to break the problem into pieces or modules. Each module can be separately developed and debugged Modules can then be assembled and tested to build the solution to the entire problem When programming in C++ it is often useful to write each module as one or more functions (process oriented) or methods (object oriented). A function is an independent program that solves a problem. That problem is often a component of a larger problem.
© Janice Regan, CMPT 128, Jan 2007 3 Modularization Any module, method, or function can use (call) any other function When designing a process oriented solution for a larger problem it is often useful to draw a map of which functions call which other functions (structure chart or module chart) A function can even call itself. Such a function is called a recursive function and requires special care to design Beware of unintended recursion (most commonly function1 calling function2 which calls function1, this type of recursion is also called indirect recursion). This can cause serious problems.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
© Janice Regan, CMPT 128, Jan 2007 4 Module Chart fun5 fun2 fun9 fun8 fun4 fun7 fun6 fun3 fun1 main recursion Indirect recursion
© Janice Regan, CMPT 128, Jan 2007 5 Types of functions: predefined Functions can be predefined functions Functions provided by the C++ language (or other programming language) to accomplish common tasks Trigonometric and mathematical functions Input and output functions

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
© Janice Regan, CMPT 128, Jan 2007 6 Types of functions: user defined Functions can be "user defined" functions Written by the user for applications specific to the developers needs Part of libraries associated with particular tools being used by the developer to build more complex applications Graphics libraries Statistical analysis libraries
© Janice Regan, CMPT 128, Jan 2007 7 Declaring a function A function can be called from your main function (or from any other function). Refer to the function (e.g. main) that asks for the function as the calling function Refer to the function that is being asked for as the called function. When you use C++ you should declare all the functions you plan to call at the beginning of the file containing your program just before the statement int main( )

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
© Janice Regan, CMPT 128, Jan 2007 8 Declaration A declaration is a statement describing the function that you wish to call in your program A declaration is used by the compiler to match the called function being requested with a user defined function or a predefined function from a library A declaration does not include the code implementing the body of the called function
© Janice Regan, CMPT 128, Jan 2007 9

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 29

lecture10 Functions - CMPT 128 Introduction to Computing...

This preview shows document pages 1 - 10. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online