Chapter5 - Modular Programming with Functions Engineering...

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

View Full Document Right Arrow Icon
Engineering Problem Solving with C++, Second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 5 Modular Programming with Functions Modular Programming with Functions ± Modularity ± Programmer Defined Functions ± Parameter Passing Engineering Problem Solving with C++, Second edition, J. Ingber 2 MODULARITY Engineering Problem Solving with C++, Second edition, J. Ingber 3 Modularity ± A problem solution contains numerous functions. main () is a programmer defined function. main () often references functions defined in one of the standard libraries. main () can also call other programmer defined functions. ± Functions, or modules, are independent statement blocks that are written to perform a specialized task. Engineering Problem Solving with C++, Second edition, J. Ingber 4
Background image of page 1

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

View Full DocumentRight Arrow Icon
Engineering Problem Solving with C++, Second edition, J. Ingber 5 Modules ± A C++ source program can be thought of as an ordered collection of executable tasks: input data analyze data output results ± In C++ these tasks can be performed using programmer defined functions and types. Engineering Problem Solving with C++, Second edition, J. Ingber 6 Functions ± Complex problems can be broken down into sub tasks, each of which is easy to implement in C++. ± Advantages to using programmer defined functions and types, as opposed to writing the entire solution in main (): Multiple programmers Testing/Debugging/Maintaining Reduce duplication of code Engineering Problem Solving with C++, Second edition, J. Ingber 7 Functions ± Pre-defined standard libraries ± User defined Engineering Problem Solving with C++, Second edition, J. Ingber 8 Pre-defined Functions - Example #include <iostream> #include <cmath> using namespace std; int main() { double angle; cout << “input angle in radians: “; cin >> angle; cout << “\nthe sine of the angle is “ << sin(angle) << endl; return 0; }//end main
Background image of page 2
PROGRAMMER DEFINED FUNCTIONS function definiton function prototype Engineering Problem Solving with C++, Second edition, J. Ingber 9 Engineering Problem Solving with C++, Second edition, J. Ingber 10 Functions: Terminology ± Function Prototype describes how a function is called ± Function Call references a function ± Function Arguments used in the function call ± Function Definition function header statement block ± Formal Parameters used in function definition ± Formal parameters must agree with arguments in order , number and data type , but the identifies can be different. Engineering Problem Solving with C++, Second edition, J. Ingber 11 Programmer Defined Functions ± Can be defined to: return a single value to the calling function. perform a data independent task.
Background image of page 3

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

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 11

Chapter5 - Modular Programming with Functions Engineering...

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

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