190 likes | 290 Views
CMPT 128: Introduction to Computing Science for Engineering Students. Functions (1). 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
E N D
CMPT 128: Introduction to Computing Science for Engineering Students Functions (1)
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.
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.
main fun1 fun2 fun3 fun4 fun5 fun6 fun7 Indirect recursion fun8 fun9 recursion Module Chart
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
Types of functions: user defined • Functions can be "user defined" functions • Written by the developer 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
Calling function • A function can be called from your main function (or from any other function). • Refer to the function (e.g. main) that asks to use another function as the calling function • Refer to the function that is being asked for as the called function.
Function prototypes • When you use a C or C++ function you should declare the function at the beginning of the file containing your program. The declaration of a function is called a function prototype #include <iostream> using namespace std; double myFuntion( intmyData ); int myFunction2( double A, int B, int c); int main( ) { ….
Prototype • A functionprototypeis a statement describing the function that you wish to call in your program • A function prototype is used by the compiler to match the function called in the program to a function that is later user defined or a predefined function from a library • A function prototype does not include the code implementing the body of the called function
When to write function prototypes • Every function called in a program needs to have a prototype • However, sometimes you do not have to write the function’s prototype yourself • If the function is part of a library then you will have a statement in your program like #include <library> • For functions in such libraries the prototypes will be provided for you, you do not need to write them • HOW???
Provided function prototypes • If the function is part of a library then you will have a statement in your program like #include <library> • The C++ preprocessor replaces this preprocessor statement with a list of the prototypes, one fore each of the functions in the library • Then the compiler compiles your code • Therefore, before your code compiles it includes prototypes of all functions in the library
Predefined functions • For simplicity begin by considering predefined functions. • No need to write your own function prototypes • Prototypes are supplied by #Include of the library • No need to write your own definition of the function • The definition of the function (the code to implement the function) exists in the library • So how are predefined functions used?
Using predefined functions • Several libraries are included as part of C++. These libraries contain many useful tools such as mathematical functionsand I/O support • For example in cmath, you will find functions • to take the square root of a double number double sqrt(double x); • To calculate the sine of a number of radians double sin(double x);
Predefined function sqrt() double sqrt(double x); // prototype theRoot = sqrt(25.0); // use sqrt identifier (name) of library function25.0 value of formal argument x is actual argument 25.0 • C or C++ expression sqrt(x) has the value • The value of the expression sqrt(25.0) is 5.0 • 5.0 is placed in the double variable theRoot
Predefined function sin() double sin(double x); // prototype double quantity = 25; sinValue= sin( quantity ); // use sin identifier (name) of library functionquantitythe value of this formal argument x is the value of the double variable quantity (the actual argument) You can use any double variable in your calling function as the actual argument. The value of that double variable will be the input to the function
Reusing predefined functions double sin(double x); // prototype double quantity=2.5; double bone=3.6; sinValue1 = sin( quantity ); // use sinValue2 = sin( bone ); • Within a single program you can call the function any number of times using any number of different actual arguments (like quantity or bone) • Within a single program you can call the function any number of times using the same actual argument (same variable). The value of the variable that is the actual argument may change from call to call
Predefined functions >1 argument double pow(double base, int power); double quantity = 25.5; int bone = 12; powValue= pow( quantity, bone ); • You must have the same number of arguments in your call to the function as there are arguments in the prototype for the function • Arguments in your call to the function must be in the same order as the arguments in the declaration of the function. • Each argument in your call to the function must have the same type as the corresponding argument in the declaration
Predefined Functions: types boolisless (double x, double y); // returns true if x<y double x = 23.4; double y = 44.1; if ( boolValue = isless( a1, b1)) { cout << “a1 < b1” ; } • Functions can return values of different types. This function returns a boolean value. The previous examples returned double values • Functions can be used as parts of more complicated expressions
Predefined Functions • C++ Libraries contain many predefined functions for you to use. These functions can be divided into two types, both types may or may not have arguments • Those that return a value of a particular data type • We have just seen several examples of these • Those that do not return a value (void) void exit (int status);// prototype exit(1); // use