Presentation is loading. Please wait.

Presentation is loading. Please wait.

各題答對人數.

Similar presentations


Presentation on theme: "各題答對人數."— Presentation transcript:

1 各題答對人數

2 成績分佈

3 清明 ~ 杜牧 清明時節雨紛紛, 路上行人欲斷魂。 借問酒家何處有? 牧童遙指杏花村。

4 Chapter 5 Functions

5 Examples of Functions y = f(x) = 2 x3 -3 x2 -3 x + 2
f(x) = sin(x) / cos(x) g(x) = sin(x) / x

6 Why Do You Need Functions
A function is a self-contained block of code with a specific purpose. Sometimes you have to repeat the same task several times in a program. With functions, you don’t need to replicate the same code at various points. Decompose a large program into smaller functions makes it easily manageable for development and testing. A typical program will consist of a large number of small functions, rather than a small number of large functions. void draw_stars(void)

7 Decompose A Program int main() { show_title(); show_intro(); input(); process(); output(); return 0; } ============ HW1: Sorting This program will require the user to input 10 integers and sort them in ascending order. Now getting 10 integers from the user. Exchange Sort ... Print out the sorted sequence.

8 Example of Repeated Tasks (1)
#include <iostream> using std::endl; using std::cout; int main() { for (int j=1; j<=5; j++) for (int i=0; i<j; i++) cout << '*'; cout << endl; } for (int j=5; j>=1; j--) return 0; * ** *** **** *****

9 Example of Repeated Tasks (2)
#include <iostream> using std::endl; using std::cout; int print_stars(int n) { for (int i=0; i<n; i++) cout << '*'; cout << endl; return 0; } int main() for (int j=1; j<=5; j++) print_stars(j); * ** *** **** *****

10 Example of Repeated Tasks (3)
#include <iostream> using std::endl; using std::cout; int print_stars(int n) { for (int i=0; i<n; i++) cout << '*'; cout << endl; return 0; } int main() for (int j=1; j<=5; j++) print_stars(j); for (int j=5; j>=1; j--) * ** *** **** *****

11 Structure of a Function
// Function to calculate x to the power n double power(double x, int n) { double result = 1.0; for (int i = 1; i<=n; i++) result *= x; return result; } Function Header Function Body

12 type of the return value
The Function Header double power ( double x, int n ) The name of a function is governed by the same rules as those for a variable. A function returns either a single value, or nothing at all (void). The single value returned can be a pointer, which contains the address of an array. type of the return value function name parameters

13 Arguments vs. Parameters (P.180)

14 The Function Body { double result = 1.0; for (int i = 1; i<=n; i++)
result *= x; return result; } The return Statement Return a value (evaluated from an expression) to be the functional value. If the type of return value of this function is declared as void, there must be no expression. return;

15 Using a Function Define the function before it is called.
However, many programmers prefer to see main() earlier to have a global view. Declare the function using a statement called a function prototype. void print_stars() { cout << "*********" << endl; } int main() print_stars(); cout << "Test" << endl;

16 Define Your Function Based on Existing Functions
// 四捨五入 #include <iostream> #include <cmath> int round(float x) { return floor(x + 0.5); } void print(int n) std::cout << n << std::endl; return; int main() print( round(4.4) ); print( round(4.5) ); return 0;

17 Function Prototypes (P.184)
It contains the same information as appears in the function header, with the addition of a semicolon (;). double power(double value, int index); void print_stars(); You can even omit the names of the parameters double power(double, int); However, it is better to use meaningful name in a prototype to increase readability.

18 Example of Using Function Prototypes
void print_stars(); int main() { print_stars(); cout << "Test" << endl; } void print_stars() cout << "*********" << endl;

19 Using a Function Ex5_01.cpp on P.185
Note the 3 ways to call this function: Passing constants as arguments power(5.0, 3) Passing expressions (variables) as arguments power(3.0, index) Using a function as an argument power(x, power(2.0, 2.0))

20

21 You May Separate Your Code into Several Files
CC test.cpp stars.cpp -o test.exe #include <iostream> using std::cout; void print_stars(); // function prototype int main() { cout << "Good morning.\n"; print_stars(); cout << "Good afternoon.\n"; } test.cpp stars.cpp #include <iostream> void print_stars() { std::cout << "******\n"; }

22 Define function prototypes in a header file
void print_stars(); stars.h #include <iostream> using std::cout; #include "stars.h" int main() { cout << "Good morning.\n"; print_stars(); cout << "Good afternoon.\n"; } test.cpp Compile with the same command: g++ test.cpp stars.cpp -o test.exe

23 Passing Arguments to a Function
There are two mechanisms in C++ to pass arguments to functions Pass-by-value Pass-by-reference

24 Pass-by-value Copied of arguments are stored in a temporary location in memory. This mechanism protect your caller arguments from being accidentally modified by a rogue function.

25 A function may modify the parameters
void stars(int n) { while (n > 0) cout << '*'; --n; } cout << endl; int main() int i = 5; stars(i); cout << i << endl;

26 Ex5_02.cpp on P.190 int main(void) { int num = 3; cout << endl
<< "incr10(num) = " << incr10(num) << endl << "num = " << num; cout << endl; return 0; } int incr10(int n) { n += 10; cout << "In the function, n = " << n << endl; return n; num 3 copy n 3 13 Replace “num” with “n” in incr10(). This may makes Ex5_02.cpp more clear.

27 Exercise: ch5-box.cpp Design a function void box(int n) to draw a box, which has n stars on each edge.

28 Exercise: bool isDigit(char c)
Design a function bool isDigit(char c) which returns true when c is a decimal digit character ‘0’, ‘1’, ‘2’, …, ‘9’. You may test your function with the following main program: int main() { char str[] = "1A2B.g0v"; for (int i=0; i< strlen(str); i++) if ( isDigit(str[i]) ) cout << "True" << endl; else cout << "False" << endl; return 0; }

29 Pointers as Arguments to a Function
Ex5_03.cpp on P.191 int incr10(int *num); // Function Prototype int* pnum = &num; // Pointer to num *num += 10; return *num; // de-reference the pointer to get the return value // Replacing “num” with “p” in incr10() may make it more clear.

30 Ex5_02.cpp vs. Ex5_03.cpp num 3 copy num 3 13 n 3 13 p 0012FF6C 3
de-reference n += 10 *p += 10

31 Passing Arrays card_shuffle.cpp
The pointer to the beginning of the array is passed by value to the function. Ex5_04.cpp on P.192 double average(double array[], int count); average(values, (sizeof values)/(sizeof values[0])); sizeof values = ? sizeof values[0] =? card_shuffle.cpp

32 Exercise: Passing a String
Design a function show_name_box(char* s) which takes a string as the parameter, and print a line of equal signs (‘=‘) before and after it. Test your program with the main program: int main() { char* name[] = { "Alice", "Bob", "Charlie", "Dvorak" }; int i; for (i=0; i < sizeof(name) / sizeof(name[0]); i++) show_name_box(name[i]); return 0; } The result may look like ===== Alice === Bob ======= Charlie ====== Dvorak

33 Exercise P.217 Ex.2 Write a function that swaps two integers, using pointers as arguments. Write a program that uses this function and test that it works correctly. int main() { int i, j; cout << "Input two integers -- "; cin >> i >> j; cout << i << j << endl; swap(&i, &j); return 0; }

34 Exercise: Conversion to Lowercase
Design a function void ToLower(char* s) which will convert the characters in the input argument from uppercase to lowercase. Test your function with the following main program: int main() { char buffer[80]; cout << "Please input a word -- "; cin >> buffer; ToLower(buffer); cout << buffer << endl; return 0; }

35 What Is a Reference? A reference is an alias for another variable (Chapter 4, P.168). long number = 0; long& rnumber = number; rnumber += 10; cout << number; Difference between a pointer and a reference: A pointer needs to be de-referenced A reference is an alias. There is no need for de-referencing.

36 Pass-by-reference Remember that a reference is merely an alias.
Ex5_07.cpp on P.197 The output shows that the function incr10() is directly modifying the variable passed. This mechanism allows a function to modify the value of an argument in main(), just like the pointer in Ex5_03.cpp (P.191). However, passing by reference makes the program look for elegant.

37 A function can modify the parameters
void stars(int& n) { while (n > 0) cout << '*'; --n; } cout << endl; int main() int i = 5; stars(i); cout << i << endl;

38 Static Variables in a Function (P.212)
With only “automatic” variables within a function, you can’t count how many times a function is called. Within a function, you can declare a variable as static so that its value persists from one call to the next. Initialization of a static variable within a function only occurs the first time that the function is called. Ex5_14.cpp on P.213

39 Recursive Function Calls (P.214)
Recursive function - A function calls itself Either directly or indirectly fun1  fun2  fun1 Fibonacci sequence: F(n) = F(n-1) + F(n-2) F(0) = 0, F(1) = 1 Factorial N! = N*(N-1)*(N-2)*…*3*2*1 F(n) = n * F(n-1) F(0) = 1 Be sure to specify the boundary condition to stop the recursive call!

40 Fibonacci Sequence Compare with the one implemented as an array
Implemented as a recursive function // ch5_fibonacci.cpp #include <iostream> using std::cout; using std::endl; int f(int n) { if (n == 0) return 0; if (n == 1) return 1; return f(n-1) + f(n-2); // recursive call } int main() for (int i=0; i<20; i++) cout << f(i) << ' '; cout << endl; Fibonacci Sequence Compare with the one implemented as an array

41 Factorial (P.217 Ex.1) // factorial.cpp #include <iostream> using std::cout; using std::endl; int f(int n) { if (n <= 0) return 1; else return f(n-1)*n; } int main() for (int i=1; i<=10; i++) cout << i << "! = "; cout << f(i) << endl; return 0; 1! = 1 2! = 2 3! = 6 4! = 24 5! = 120 6! = 720 7! = ! = ! = ! =

42 Decimal → Octal 2310 → 278 8810 → 1308 12810 → 2008 88 % 8 oct(88 / 8)

43 ch5_dec2oct.cpp #include <iostream> using std::cout; using std::endl; void oct(int n) { if (n >= 8) oct( n / 8); // recursive call cout << n % 8; } int main() oct(23); cout << endl; oct(88); cout << endl; oct(128); cout << endl; return 0; 27 130 200

44 Exercise Convert decimal to hexadecimal by a recursive function.
You may test your function with the following main program. int main() { hex(23); cout << endl; hex(88); cout << endl; hex(127); cout << endl; hex(128); cout << endl; hex(255); cout << endl; hex(256); cout << endl; return 0; }


Download ppt "各題答對人數."

Similar presentations


Ads by Google