520 likes | 535 Views
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.
E N D
Cairo University, Faculty of Computers and Information CS112 – 2017 / 2018 2nd TermProgramming ILecture 9: C++ Functions I By Dr. Mohamed El-Ramly
Lecture ObjectivesC++ Functions • 1- Structured Programming • 2- Function Definition • 3- Function Call • 4- Passing Parameters by Value • 5- Passing Parameters by Reference
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.
Review - CString • sizeof vs strlen • chara[10]="Sea"; • cout << sizeof a << endl; // 10 • cout << strlen(a) << endl; // 3
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")
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;
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.
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.
I. Structured Programming Main Initialize Game Make a Move Display Board Calculate Winner
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.
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.
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.)
II. User-Defined Functions • C++ programs usually have the following form: // include statement // function prototypes // main() function // function definitions
Function Definition A function definition has the following syntax: <type> <function name>(<parameter list>) { <local declarations> <sequence of statements> }
Function Definition Function to calculate the absolute value int absolute(int x){ return(x >= 0 ? x : -x); } num num get absolute
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.
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);
III. Function Call • A function call has the following syntax: <function name>(<argument list>) • Example: • int distance = absolute(-5);
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);
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); }
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; }
voidFunctions void print_square (int side) { ....... ....... ....... } • These are functions that do the work inside, maybe take some parameters, but do not return any value. ****** * * * * * * * * ******
Pb#16: Diamond Shape • Write a function to print the diamond shape of a given size, e.g. * * * *** *** *** ***** ***** ***** *** ******* ******* * ***** ********* *** ******* * ***** *** *
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 ; } }
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
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!
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
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
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
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
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
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; }
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);
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
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
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
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
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
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; }
Readings • Problem Solving with C++, Walter Savitch • Chapter 2,3 and 4