Functions User-defined functions Top-Down Design Abstraction Local Variables (Scope of variables) Library Functions Library Functions Library Functions Page 1 5/29/2016 CS3369 Real Time Control Software/WANG Lusheng Top-Down Design: break the task of the program to subtasks. solve each subtask by a sequence of statements. In C++ a sequence of statements can be implemented by a function • One can use one statement to represent a sequence of statements. Page 2 5/29/2016 CS3369 Real Time Control Software/WANG Lusheng #include<iostream.h> #include<dos.h> #include<conio.h> void main() { int i, x=1; for(x=1; x<=72; x++) { clrscr(); for(i=1; i<=x;i++) cout<<" "; cout<<" * \n "; for(i=1; i<=x; i++) cout<<" "; cout<<" * \n"; for(i=1; i<=x; i++) cout<<" "; cout<<"*********\n"; for(i=1; i<=x; i++) cout<<" "; cout<<" * \n"; for(i=1; i<=x; i++) cout<<" "; cout<<" * \n"; delay(300); } 5/29/2016 } #include<iostream.h> void draw_plane( int x) { int i; void draw_plane(int y); for(i=1; i<=x;i++) cout<<" "; /* The prototype */ cout<<" * \n "; for(i=1; i<=x; i++) void main() cout<<" "; { int x; cout<<" * \n"; for(x=1; x<=72; x++) for(i=1; i<=x; i++) { clrscr(); cout<<" "; draw_plane(x); cout<<"*********\n"; delay(300); for(i=1; i<=x; i++) } cout<<" "; cout<<" * \n"; for(i=1; i<=x; i++) cout<<" "; cout<<" * \n"; } #include<dos.h> #include<conio.h> Page 3 CS3369 Real Time Control Software/WANG Lusheng Call the same function twice, we can show two air planes. #include<iostream.h> #include<dos.h> #include<conio.h> void draw_plane(int y); /* The prototype */ void main() { int x; for(x=1; x<=72; x++) { clrscr(); draw_plane(x); if(x>=19) draw_plane(x-18); delay(300); void draw_plane( int x) { int i; for(i=1; i<=x;i++) cout<<" "; cout<<" * \n "; for(i=1; i<=x; i++) cout<<" "; cout<<" * \n"; for(i=1; i<=x; i++) cout<<" "; cout<<"*********\n"; for(i=1; i<=x; i++) cout<<" "; cout<<" * \n"; for(i=1; i<=x; i++) cout<<" "; cout<<" * \n"; } /*1st plane is 18 columns away */ } 5/29/2016 } Page 4 CS3369 Real Time Control Software/WANG Lusheng Some Discussion about Functions 1. In C++ a sequence of statements can be implemented by a function 2. User can use (call) a function many times so that the programs become shorter. 3. A function provides a tool to use/repeat a sequence of statements flexibly, i.e., each use (call) of the same function does similar but different thing. (See Slide 4 for an example.) 4. A function corresponds to some subtask in top down design so that the design of the whole program becomes easier. 5/29/2016 CS3369 Real Time Control Software/WANG Lusheng Page 5 User Defined Functions Function prototypes:(what are the inputs and outputs) return_type function_name (parameter_list); • here parameter_list is either empty or – type_1 parameter_1; parameter_list; – //this is a recursive definition • the function prototypes should be given before main(). (see examples) Function definitions: return_type function_name (parameter_list) {statements; return a_value_of_return_type;} Page 6 5/29/2016 CS3369 Real Time Control Software/WANG Lusheng Function Calls Whenever one needs to use a function, one calls the function: assign values to the variables in parameter_list • Example: draw_plane(x-18); match return_type in function call assignment. • If your function returns a value, assignment this value to a variable of the same type. Example: Write a program that asks the user to input two integers from the keyboard and output the maximum of the two integers. Page 7 5/29/2016 CS3369 Real Time Control Software/WANG Lusheng An Example #include <iostream.h> int max(int x, int y); int main() { int a, b; int x; cout <<“input two integers\n”; cin >> a >> b; x = max(a,b); cout <<“the maximum is” <<max(a,b); return 0; } int max (int x, int y) { int tmp=x; if (tmp<y) tmp=y; return tmp;} 5/29/2016 //input&output library functions //prototype of function max; //the main function //define two integers //define another integer //ask for two integers //input two integers call the function max() //output their maximum //return 0 for main() //end of main //definition of max(x,y) //initialize tmp to be x //if y is bigger, let tmp=y //return tmp at the end of max Page 8 CS3369 Real Time Control Software/WANG Lusheng A function definition is like a small program and a function call is the same thing as running this program. A function uses formal parameters, instead of “cin”, for input. The arguments to the function are the input and they are plugged in for the formal parameters. The output of a function is send back to the program which calls this function, via the return value. The function uses the return statement for output purpose. Page 9 5/29/2016 CS3369 Real Time Control Software/WANG Lusheng Procedure Abstraction Blackbox analogy: Others should know how to use a function by looking at the function prototype and its accompanying comment without looking at the body of the function definition. Prototype comment should include all the conditions required of the arguments to the function and should describe the value that is returned. All variables used in the function body should be declared in the function body (the formal parameters do not need to be declared in the body since they are listed in the function prototype.) Page 10 5/29/2016 CS3369 Real Time Control Software/WANG Lusheng Shooting a Bullet using functions #include <math.h> #include<dos.h> #include<stdio.h> #include <graphics.h> #include<conio.h> void shoe(int i); void erease(int i); void main(void) { int driver =DETECT,mode; int i,j; int x=1,y=1; initgraph(&driver,&mode,"a:\\bgi"); setcolor(WHITE); line(1,400,400,400); for ( i = 0; i < 80; i++ ) { show(i); delay(300); erease(i); } closegraph(); } void show(int i) { x=5*i; /* x=i; */ y=9*i-0.15*i*i; /* y=i; */ setcolor(RED); circle(400-x,400-y,2); } void erease(int i) { x=5*i; /* x=i; */ y=9*i-0.15*i*i; /* y=i; */ setcolor(BLACK); circle(400-x,400-y,2); } Page 11 5/29/2016 CS3369 Real Time Control Software/WANG Lusheng Shooting Bullets using functions #include <math.h> #include<dos.h> #include<stdio.h> #include <graphics.h> #include<conio.h> void shoe(int i); void erease(int i); void main(void) { int driver =DETECT,mode; int i,j; int x=1,y=1; initgraph(&driver,&mode,"a:\\bgi"); setcolor(WHITE); line(1,400,400,400); for ( i = 0; i < 80; i++ ) { show(i); if (i>=11) show(i-10); delay(300); erease(i); if (i>=11) erease(i-10); void show(int i) { x=5*i; /* x=i; */ y=9*i-0.15*i*i; /* y=i; */ setcolor(RED); circle(400-x,400-y,2); } void erease(int i) { x=5*i; /* x=i; */ y=9*i-0.15*i*i; /* y=i; */ setcolor(BLACK); circle(400-x,400-y,2); } } closegraph(); } 5/29/2016 Page 12 CS3369 Real Time Control Software/WANG Lusheng Shooting Bullets in different directions/speeds #include <math.h> #include<dos.h> #include<stdio.h> #include <graphics.h> #include<conio.h> void shoe(int i, float h, float v); void erease(int i, float h, float v); void main(void) { int driver =DETECT,mode; int i,j; int x=1,y=1; initgraph(&driver,&mode,"a:\\bgi"); setcolor(WHITE); line(1,400,400,400); for ( i = 0; i < 80; i++ ) { show(i,5.0, 9.0); if (i>=11) show(i-10, 10.0, 8.0); delay(300); erease(i, 5.0, 9.0); if (i>=11) erease(i-10, 10.0, 8.0); void show(int i, float h, float v) { x=h*i; /* x=i; */ y=v*i-0.15*i*i; /* y=i; */ setcolor(RED); circle(400-x,400-y,2); } void erease(int i, float h, float v ) { x=h*i; /* x=i; */ y=v*i-0.15*i*i; /* y=i; */ setcolor(BLACK); circle(400-x,400-y,2); } } closegraph(); } Page 13 5/29/2016 CS3369 Real Time Control Software/WANG Lusheng Shooting Bullets in different directions/speeds #include <math.h> #include<dos.h> #include<stdio.h> #include <graphics.h> #include<conio.h> void shoe(int i, float h, float v); void erease(int i, float h, float v); int vertical(int i, float v, float acc); void main(void) { int driver =DETECT,mode; int i,j; int x=1,y=1; initgraph(&driver,&mode,"a:\\bgi"); setcolor(WHITE); line(1,400,400,400); for ( i = 0; i < 80; i++ ) { show(i,5.0, 9.0); if (i>=11) show(i-10, 10.0, 8.0); delay(300); erease(i, 5.0, 9.0); if (i>=11) erease(i-10, 10.0, 8.0); } closegraph(); } 5/29/2016 void show(int i, float h, float v) { x=h*i; /* x=i; */ y=v*i-0.15*i*i; /* y=i; */ setcolor(RED); circle(400-x,400-y,2); } void erease(int i, float h, float v ) { x=h*i; y=vertical (i, v, 0.15); /* y=v*i-0.15*i*i; */ setcolor(BLACK); circle(400-x,400-y,2); } int vertical(int i, float v, float acc) { int y; y= v*i-acc*i*i; return y; } A defined function can call another defined function. Page 14 CS3369 Real Time Control Software/WANG Lusheng Top-Down Design: break the task of the program to subtasks. solve each subtask by a sequence of statements. Shooting a Bullet Shooting two bullets 1. Show a bullet at a specified position 1. Show a bullet at a specified position. 2. Hold the figure for a while (delay) 3. Erase the bullet at the same position 4. Repeat Steps 1-3 for 80 times, each with a different position. 2. Show another bullet if the 1st bullet is far enough. 3. Hold the figure for a while 4. Delete the two bullets 5. Repeat Steps 1-4 for 80 times. Remarks: Logically, the programs are very simple, though they look like sophisticated. Page 15 5/29/2016 CS3369 Real Time Control Software/WANG Lusheng