Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 Functions and Libraries

2 The idea of a function

3 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).

4 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.

5 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; }

6 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.

7 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)

8 Defining functions in C++ type name(parameters) {... body... } ตัวอย่าง predicate function bool isEven(int n) { return n % 2 == 0; } ตัวอย่างการเรียกใช้ predicate function If (isEven(i))...

9 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.

10 Overloading int abs(int x) { return (x < 0) ? -x : x; } double abs(double x) { return (x < 0) ? -x : x; }

11 Default parameters Default parameters are parameters should have if they do not appear in the call. ตัวอย่างการประกาศใน function prototype void formatInColumns(int nColumns = 2); ตัวอย่างการเรียกใช้ formatInColumns();

12 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.

13 Bad using default parameter void setInitialLocation(double x, double y); void setInitialLocation() { setInitialLocation(0, 0); } ตัวอย่างการใช้งานที่ไม่ดีเพราะสามารถเรียก ฟังก์ชันด้วยอาร์กิวเม้นท์ตัวเดียว void setInitialLocation(double x = 0, double y = 0);

14 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.

15 The combinations function

16 /* * 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);

17 /* 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; }

18 /* * 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)); }

19 /* * 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; }


Download ppt "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."

Similar presentations


Ads by Google