FunctionsUser-defined functionsTop-Down DesignAbstractionLocal Variables (Scope of variablesLibrary FunctionsLibrary FunctionsLibrary Functions
User-defined functions Top-Down Design Abstraction Local Variables (Scope of variables) Library Functions Library Functions Library Functions Functions
Top-DownDesignbreak thetask of the program to subtaskssolve each subtask by a sequence of statementsIn C++ a sequence of statements can be implemented byafunction.One can use one statement to represent a sequence ofstatements
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
#include<iostream.h#include<iostream.h#include<dos.hyvoid draw plane(intx)#include<dos.h>一#include<conio.h>#include<conio.h>intivoidmainOfor(i-1,i-x,i++)void draw plane(inty);cout<""./*The prototype*/int i, x-1,cout<k"*In",for(x=1:x<=72:x++)for(i=l,=x i++)voidmain2cout<<nn.Cintx,elrserO),cout<<*In"for(x-1:x<-72,x++)for(i=1,i<-xi++)for(i-l:i<-x i++)clrscrO,cout<<""cout<<""draw_plane(x),cout<<"*In"cout<<"*********\n".delay(300),for(i-1i<=x i++)for(i=1:<-x.it+)cout<<"".coutk<nn.cout<*In"cout<"*In"for(i=,<-x i++)for(i-l:<-xi++)cout<coutk<"".cout<<"*********/n”cout*In"for(i-1:i<=x,i++)亨cout<"".cout<<"*In",for(i-l:i<=x i++)cout<<""*cout<nIn":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); } } #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); } 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"; }
void draw plane(int x)Call the samefunctionintitwice,we can showtwofor(i-1 K-xi++)air planes.cout<<"cout<<"*In"#include<iostream.h>for(i=-1:=x+)#include<dos.h>cout<<"n.#include<conio.h>cout<<"*In"voiddraw plane(inty):for(i=l<=x i++)/*The prototype */coutk<""voidmainOCout<<"*********\n"Cintx,for(i=l:<=x.i+)cout"for(x-1:x<=72:x++)cout<"*Inclrscro,for(i-l:<-xi++)draw_plane(x),cout<<""if(x>-19)cout<"*An"drawplane(x-18),手delay(300);/*1stplaneis18 columns away *
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 */ } } 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"; }
Some Discussion about Functions1.In C++a sequence of statements can be implementedbyafunction2.User can use(call) afunctionmany times so thattheprograms become shorter3.A function provides a tool to use/repeat a sequence ofstatements flexibly,i.e.,eachuse (call)ofthe samefunction does similar but different thing.(See Slide 4 foranexample.)4.A function corresponds to some subtask intop downdesign so that the design of the whole program becomeseasier
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