(CHAP Using Functions CHAP)
Programs nowadays can be millions of lines long. You will certainly meet ones with thousands of lines. How do humans understand such complex things? They split them into sections, and look at each section in turn. This is one role of functions.
Additionally, C++ comes with libraries of pre-written functions, which we can easily incorporate in our programs. This is the second role - re-use.
Without a collection of functions, a programming language can be ineffective. Fortunately, C++ does well for functions. Note that in some other languages, functions might be called procedures or subroutines.
A library of functions is a xxrelated collection of functions. For example, we have a maths library, a string library, and an I/O library. A C++ system comes with the 'standard library' which is really a collection of such libraries, around 50 in all.
There is also the area of 3rd-party libraries. Software products often come with a library to allow access and adapting the product. They are often usable from C++ and C. C++ can access C libraries, so that opens the C++ programmer up to huge resources. For example, the user-interface toolkit called GTK+ comes with hundreds of functions that can be used from C++.
The bottom lines are:
(SEC Function Arguments, and the result
An argument is an input to a function. (The word is used in a mathematical sense - nothing to do with a disagreement).
To use a function (often we say 'call') you need to find out how many arguments, and what type they are. We provide some of this information here, but in reality you would often look it up via Google. Here is an example, involving square-root. The sqrt function is in the xxcmath library, and we must xxinclude this at the top of the program. We find out that it takes one float argument.
We also find out that it returns a result, which we have to deal with somehow.
For example, we might put: /code n = sqrt(9.0); code/ 9.0 is the argument, and the answer (3.0) is assigned to xxn.
Here is a complete program, showing more possibilities:
/code //function demo #include <iostream> #include <string> #include <cmath> //or <math.h> using namespace std;
int main(){ string line = "Some text in a string."; string s; float f = 9.6; float g; int i = 9; int j, k; g = sqrt(f); cout << "square root of f is " << g << endl; j = 10 * sqrt(i) + 1; //part of an expression k = 5 * sqrt(i + 7); //expression for an argument cout << "j is " << j << ", k is " << k << endl; //strings s = line.substr(1,6); //start at 1, 6 characters cout << s << endl; // ome te (6 characters) cout << "Length is " << line.length() << endl; return 0; } code/
It produces:
/code square root of f is 3.09839 j is 31, k is 20 ome te Length is 22 code/
Here is an overview of the above program:
(SEC Functions - The Dot Notation
Traditionally, to use a function, you only need to know its name, and the type of data it accepted and returned. Around 1985, things got more powerful, and slightly more complicated. C++ introduced classes of objects - such as strings, and functions associated with strings had to be called in this way: /code object-name.function(arguments) code/ contrasted to /code function(variable-name, arguments) code/
The dot is similar to the apostrophe use in, for example:
Mary's coat.
We access the line's length by using xxline.xxlength.
(SEC The Number And Type of Arguments
Consider the xxsqrt function - it takes in a float value. Here is a mixture of correct and incorrect calls:
/code float y = 1.234; float x; int y = 9; if x>=6 and y<10 then test html ; here
x = sqrt("abc"); //no - can't work with string x = sqrt("1234"); //no - "1234" is a string x = sqrt(); //no - missing argument x = sqrt(23, 33); //no - 2 arguments given x = sqrt; //no - missing brackets
x = sqrt(y); //yes - works with int x = sqrt(3 * y + 1); //yes - expression as argument x = 4 * sqrt(3 * y); //yes - part of expression cout << sqrt(y); //yes - output the result code/
As we see, the number of arguments (for xxsqrt, 1) and the type of arguments (e.g. not a string) must be correct. If a function needs several arguments, we separate them with commas. If a function needs no arguments, we use empty brackets. For example, in the above program, the xxlength function needs no arguments, and the xxsubstr function needs 2 arguments.
56