150 likes | 163 Views
Learn how to break down tasks, define functions, implement local variables, and utilize library functions in this C++ programming guide.
E N D
Functions • User-defined functions • Top-Down Design • Abstraction • Local Variables (Scope of variables) • Library Functions • Library Functions • Library Functions 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. CS3369 Real Time Control Software/WANG Lusheng
#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); delay(300); } #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); } } 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"; } CS3369 Real Time Control Software/WANG Lusheng
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"; } 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); /*1st plane is 18 columns away */ } } CS3369 Real Time Control Software/WANG Lusheng
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. • Some Discussion about Functions CS3369 Real Time Control Software/WANG Lusheng
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;} 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. CS3369 Real Time Control Software/WANG Lusheng
#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;} //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 An Example 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. 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.) 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); } 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); } 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); } 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); } closegraph(); } 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); } 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(); } 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. 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 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. Shooting two bullets 1. Show a bullet at a specified 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. CS3369 Real Time Control Software/WANG Lusheng