lecture10 Functions

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

Info iconThis preview shows pages 1–9. Sign up to view the full content.

View Full Document Right Arrow Icon
1 © Janice Regan, CMPT 128, Jan 2007 CMPT 128: Introduction to Computing Science for Engineering Students Functions (1)
Background image of page 1

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

View Full DocumentRight Arrow Icon
© 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.
Background image of page 2
© 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.
Background image of page 3

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

View Full DocumentRight Arrow Icon
© Janice Regan, CMPT 128, Jan 2007 4 Module Chart fun5 fun2 fun9 fun8 fun4 fun7 fun6 fun3 fun1 main recursion Indirect recursion
Background image of page 4
© 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
Background image of page 5

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

View Full DocumentRight Arrow Icon
© 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
Background image of page 6
© 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( )
Background image of page 7

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

View Full DocumentRight Arrow Icon
© 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
Background image of page 8
Image of page 9
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 05/18/2010 for the course CMPT 128 taught by Professor Regan during the Spring '08 term at Simon Fraser.

Page1 / 29

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

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

View Full Document Right Arrow Icon
Ask a homework question - tutors are online