1 / 52

C++ Functions: Game Playing Algorithm and Function Decomposition

Learn about structured programming in C++, solving complex problems by breaking them down into smaller functions. Develop an algorithm for a two-player board game and decompose the program into suitable functions.

jlake
Download Presentation

C++ Functions: Game Playing Algorithm and Function Decomposition

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Cairo University, Faculty of Computers and Information CS112 – 2017 / 2018 2nd TermProgramming ILecture 9: C++ Functions I By Dr. Mohamed El-Ramly

  2. Lecture ObjectivesC++ Functions • 1- Structured Programming • 2- Function Definition • 3- Function Call • 4- Passing Parameters by Value • 5- Passing Parameters by Reference

  3. Pb#14: Game Playing • Develop an algorithm for playing a two-players board game. • Break down the program to suitable functions. Describe each function. • Draw the system decomposition diagram.

  4. Review - CString • sizeof vs strlen • chara[10]="Sea"; • cout << sizeof a << endl; // 10 • cout << strlen(a) << endl; // 3

  5. Review - CString • a = strcmp("sea", "sea"); • a = strcmp("tea", "sea"); • a = strcmp("sea", "tea"); • a = strcmp("sea", "t"); • a = strcmp("sea", "sun"); • a = strcmp("" , "sea"); • a = strcmp("s" , "sea"); • a = strcmp("Sea", "sea"); • a = strcmp("SEA", "sea"); • a = strcmp("sea", "set")

  6. Review - CString • char a[20] = "Sea"; • char b[] = "Desert"; • char c[20]; • strcat(a, " and "); • strcat(a, b); • Strcpy(c, a); • cout << a << endl; • cout << c << endl;

  7. I. Structured Programming • A complex problem is easier to solve by dividing it into several smaller parts, each of which can be solved by itself. • This is called structuredprogramming. • These parts are sometimes made into functionsin C++. • main()then uses these functions to solve the original problem.

  8. I. Structured Programming • It is a way of building the program by breaking it into small pieces. • Each piece (function) is a black box that takes input and (possibly) returns output. • The main function calls the services of other functions and they call each other.

  9. I. Structured Programming Main Initialize Game Make a Move Display Board Calculate Winner

  10. Advantages of Functions • Functions separate the concept (what is done) from the implementation (how it is done). • double sqrt (double) • I do not care if the function uses the Babylonian or Bakhshali’s methods. • I care that it takes double, returns double and does square root.

  11. Advantages of Functions • Functions make programs easier to understand. • Functions can be called several times in the same program, allowing the code to be reused.

  12. C++ Functions • C++ allows the use of both user-definedand external built-in functions. • Built-infunctions(e.g., abs,ceil,rand, sqrt, etc.) are usually grouped into specialized libraries (e.g., iostream, stdlib,math, etc.)

  13. II. User-Defined Functions • C++ programs usually have the following form: // include statement // function prototypes // main() function // function definitions

  14. Function Input and Output 3 1 2

  15. Function Definition A function definition has the following syntax: <type> <function name>(<parameter list>) { <local declarations> <sequence of statements> }

  16. Function Definition Function to calculate the absolute value int absolute(int x){ return(x >= 0 ? x : -x); } num num get absolute

  17. Function Definition • The function definition can be placed anywhere in the program. • If a function definition is placed in front of main(), there is no need to include its function prototype. • If it is placed after the main, a prototype must be included at the top.

  18. Function Prototype • The function prototype declares the input and output parameters of the function. • It has syntax: <type> <fun name> (<type list>); • Example: A function that returns the absolute value of an integer is: • int absolute(int);

  19. III. Function Call • A function call has the following syntax: <function name>(<argument list>) • Example: • int distance = absolute(-5);

  20. Arguments / Parameters • one-to-one correspondence between the arguments in a function call and the parameters in the function definition. int int get absolute int x = abs (-20);

  21. Absolute Value int absolute (int);// function prototype int main(){ int num, answer; cout << "Enter an integer (0 to stop): "; cin >> num; while (num!=0){ answer = absolute(num); cout << "The absolute value of " << num << " is: " << answer << endl; cin >> num; } return 0; } // Define absolute function int absolute(int x){ return (x >= 0 ? X : -x); }

  22. Function of three parameters #include <iostream> using namespace std; double total_second(int, double ,double ); int main(){ cout << total_second(1,1.5, 2) << endl; return 0; } double total_second ( int hour, double minutes, double second) { return hour*3600 + minutes * 60 + second; }

  23. voidFunctions void print_square (int side) { ....... ....... ....... } • These are functions that do the work inside, maybe take some parameters, but do not return any value. ****** * * * * * * * * ******

  24. Pb#16: Diamond Shape • Write a function to print the diamond shape of a given size, e.g. * * * *** *** *** ***** ***** ***** *** ******* ******* * ***** ********* *** ******* * ***** *** *

  25. Printing Diamond Shape void diamond(int size) { int row, space, star; for(row = 1; row <= size; row++){ //top half for(space = 1; space <= size - row; space++) cout << " "; for(star = 1; star <= 2 * row - 1; star++) cout << "*"; cout << endl ; } for(row = size -1; row >= 1; row--){ //bottom half for (space = 1; space <= size - row; space++) cout << " "; for (star = 1; star <= 2 * row - 1; star++) cout << "*"; cout << endl ; } }

  26. Calculating the Area of a Circlewith a Function area?

  27. Using Parameters • Function Parameters come in two: • Value parameters – which copy the values of the function arguments • Reference parameters– which refer to the function arguments by other local names and have the ability to change the values of the referenced arguments

  28. IV. Value Parameters • This is what we use to declare in the function signature or function header, e.g. int max (int x, int y); • Here, parameters x and y are value parameters • When you call the max function as max(4, 7), the values 4 and 7 are copied to x and y respectively • When you call the max function as max (a, b),where a=40 and b=10, the values 40 and 10 are copied to x and y respectively • When you call the max function as max( a+b, b/2),the values 50 and 5 are copies to x and y respectively • Once the value parameters accepted copies of the corresponding arguments data, they act as local variables!

  29. Call by Value #include <iostream.h> void fun(int y) { cout << y << endl; y = y + 5; } void main() { int x = 3; fun(x); cout << x << endl; } 0 x void main() { x = 3; fun(x); cout << x << endl; } 1

  30. Call by Value #include <iostream.h> void fun(int y) { cout << y << endl; y = y + 5; } void main() { int x = 3; fun(x); cout << x << endl; } 3 x void fun(int x ) { cout << x << endl; x=x+5; } 3 void main() { x = 3; fun(x); cout << x << endl; } 3 2

  31. Call by Value #include <iostream.h> void fun(int y) { cout << y << endl; y = y + 5; } void main() { int x = 3; fun(x); cout << x << endl; } 3 x 3 void fun(int x ) { cout << x << endl; x=x+5; } 8 4 void main() { x = 3; fun(x); cout << x << endl; } 2

  32. Call by Value #include <iostream.h> void fun(int y) { cout << y << endl; y = y + 5; } void main() { int x = 3; fun(x); cout << x << endl; } 3 x 3 void fun(int x ) { cout << x << endl; x=x+5; } 8 5 void main() { x = 3; fun(x); cout << x << endl; } 2

  33. Call by Value #include <iostream.h> void fun(int y) { cout << y << endl; y = y + 5; } void main() { int x = 3; fun(x); cout << x << endl; } 3 x void main() { x = 3; fun(x); cout << x << endl; } 6

  34. 4 x void main() { x = 3; fun(x); cout << x << endl; } 7 #include <iostream.h> void fun(int y) { cout << y << endl; y = y + 5; } void main() { int x = 3; fun(x); cout << x << endl; }

  35. Reference Parameters • As we saw in the last example, any changes in the value parameters don’t affect the original function arguments • If we want to change the values of the original function arguments or return > one value from the function, we use reference parameters • A reference parameter is just another name to the original argument variable • We define a reference parameter by adding the & in front of the parameter name, e.g. double update (double & x);

  36. x x ? 4 Call by Reference #include <iostream.h> void fun(int& y) { cout << y << endl; y = y + 5; } void main() { x = 4; fun(x); cout << x << endl; } void main() { int x = 4; fun(x); cout << x << endl; } 1

  37. void fun( int & y ) { cout<<y<<endl; y=y+5; } x x ? 4 Call by Reference #include <iostream.h> void fun(int& y) { cout << y << endl; y = y + 5; } void main() { x = 4; fun(x); cout << x << endl; } 3 void main() { int x = 4; fun(x); cout << x << endl; } 2

  38. void fun( int & y ) { cout<<y<<endl; y=y+5; } x x ? 4 Call by Reference #include <iostream.h> void fun(int& y) { cout << y << endl; y = y + 5; } void main() { x = 4; fun(x); cout << x << endl; } 4 9 void main() { int x = 4; fun(x); cout << x << endl; } 2

  39. void fun( int & y ) { cout<<y<<endl; y=y+5; } x x ? 9 Call by Reference #include <iostream.h> void fun(int& y) { cout << y << endl; y = y + 5; } void main() { x = 4; fun(x); cout << x << endl; } 5 void main() { int x = 4; fun(x); cout << x << endl; } 2

  40. x x ? 9 Call by Reference #include <iostream.h> void fun(int& y) { cout << y << endl; y = y + 5; } void main() { x = 4; fun(x); cout << x << endl; } void main() { int x = 4; fun(x); cout << x << endl; } 6

  41. void main() { int x = 4; fun(x); cout << x << endl; } x x ? 9 7 Call by Reference #include <iostream.h> void fun(int& y) { cout << y << endl; y = y + 5; } void main() { x = 4; fun(x); cout << x << endl; }

  42. Readings • Problem Solving with C++, Walter Savitch • Chapter 2,3 and 4

More Related