Functions and Libraries
The idea of a function
Functions in programming A function is a block of code that has been given a name. To invoke that code is known as calling that function. Arguments are a list of expression enclosed in parentheses (in function call). Parameters are a list of variable enclosed in parentheses (in function definition).
The advantage of using functions Write once, use many times. Make it possible to divide a large program into smaller (called decomposition). Some functions may be complicated, it is appropriate to decompose them into smaller ones. This process is called top-down design or stepwise refinement.
Functions and algorithms Functions provide a basis for the implementation of algorithms. Euclid’s algorithm 1. Divide x by y and compute the remainder; call that remainder r. 2. If r is zero, the algorithm is complete, and the answer is y. 3. If r is not zero, set x to the old value of y, set y equal to r, and repeat the process. int gcd(int x, int y) { int r = x % y; while (r != 0) { x = y; y = r; r = x % y; } return y; }
Libraries Most of the code the computer executes is not the code you have written yourself, but the library code that you have loaded along with the application. If you want to become an effective C++ programmer, you need to spend much time learning about the standard libraries. The library exports the service, e.g. exports cin and cout streams.
Libraries An implementer is a programmer who implements a library. A client is a programmer who calls functions provided by a library. Example functions in abs(x)exp(x)cos(theta) sqrt(x)log(x)sin(theta) floor(x)log10(x)tan(theta) ceil(x)pow(x,y)atan(x)
Defining functions in C++ type name(parameters) {... body... } ตัวอย่าง predicate function bool isEven(int n) { return n % 2 == 0; } ตัวอย่างการเรียกใช้ predicate function If (isEven(i))...
Overloading Using the same name for more than one version of a function is called overloading. Signature is a pattern of arguments taken by a function (i.e. number and types of argument). The primary advantage is makes it easier for a programmer to keep track of different function names.
Overloading int abs(int x) { return (x < 0) ? -x : x; } double abs(double x) { return (x < 0) ? -x : x; }
Default parameters Default parameters are parameters should have if they do not appear in the call. ตัวอย่างการประกาศใน function prototype void formatInColumns(int nColumns = 2); ตัวอย่างการเรียกใช้ formatInColumns();
Default parameters rules The default value appears only in the function prototype and not in the function definition. Any default parameters must appear at the end of the parameter list.
Bad using default parameter void setInitialLocation(double x, double y); void setInitialLocation() { setInitialLocation(0, 0); } ตัวอย่างการใช้งานที่ไม่ดีเพราะสามารถเรียก ฟังก์ชันด้วยอาร์กิวเม้นท์ตัวเดียว void setInitialLocation(double x = 0, double y = 0);
The steps in calling a function The calling function computes values for each argument. The system create new space for all local variable of the new function which is called stack frame. The value of each argument is copied into the corresponding parameter variable. A type conversion is performed. The statements in the function body are executed until reaching a return statement. The value of the return expression is evaluated and returned as a value of a function. A type conversion is performed. The stack frame is discarded. The calling program continues.
The combinations function
/* * File: Combinations.cpp * * This program computes the mathematical function C(n, k) from * its mathematical definition in terms of factorials. */ #include using namespace std; /* Function prototypes */ int combinations(int n, int k); int fact(int n);
/* Main program */ int main() { int n, k; cout << "Enter the number of objects (n): "; cin >> n; cout << "Enter the number to be chosen (k): "; cin >> k; cout << "C(n, k) = " << combinations(n, k) << endl; return 0; }
/* * Function: combinations(n, k) * Usage: int nWays = combinations(n, k); * * Returns the mathematical combinations function C(n, k), which is * the number of ways one can choose k elements from a set of size n. */ int combinations(int n, int k) { return fact(n) / (fact(k) * fact(n - k)); }
/* * Function: fact(n) * Usage: int result = fact(n); * * Returns the factorial of n, which is the product of all the * integers between 1 and n, inclusive. */ int fact(int n) { int result = 1; for (int i = 1; i <= n; i++) { result *= i; } return result; }