1 / 29

C++ provides two kinds of subprograms: void functions value-returning functions

C++ provides two kinds of subprograms: void functions value-returning functions. Chapter 7 Functions. Functional Decomposition with Void Functions Function Parameters Syntax and Semantics of Void Functions Parameters 5. Designing Functions.

kaycee
Download Presentation

C++ provides two kinds of subprograms: void functions value-returning functions

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. C++ provides two kinds of subprograms: void functions value-returning functions

  2. Chapter 7 Functions • Functional Decomposition with Void Functions • Function Parameters • Syntax and Semantics of Void Functions • Parameters • 5. Designing Functions

  3. 1. Functional Decomposition with Void Functions Writing Modules as Void Functions Main function Void function void main( ) int main( ) void print( ) cout<<“ok”; cout<<“ok”; return 0;

  4. Functional Decomposition in Pseudocode Main Lever 0 **************** Print two lines of asterisks **************** Print “Welcome Home!” Print four lines of asterisks Welcome Home! Print 2 lines Lever 1 **************** **************** Print “****************” **************** Print “****************” **************** Print 4 lines Print “****************” Print “****************” Print “****************” Print “****************”

  5. int main( ) Main Print2lines( ); Print two lines of asterisks cout<<“Welcome Home!”<<endl; Print “Welcome Home!” Print4lines( ); Print four lines of asterisks return 0; Print 2 lines void Print2lines( ) Print “****************” cout<<“****************”<<endl; Print “****************” cout<<“****************”<<endl; Print 4 lines void Print4lines( ) Print “****************” Print “****************” cout<<“****************”<<endl; Print “****************” cout<<“****************”<<endl; Print “****************” cout<<“****************”<<endl; cout<<“****************”<<endl;

  6. include <iostream> using namespace std; void Print2lines( ); //Function prototypes void Print4lines( ); int main( ) Print2lines( ); //Function call cout<<“Welcome Home!”<<endl; Print4lines( ); //Function call return 0; //Function heading void Print2lines( ) · · · · · · void Print4lines( ) //Function heading · · · · · ·

  7. int main( ) Print2lines( ); cout<<“Welcome Home!”<<endl; Print4lines( ); return 0; void Print2lines( ) cout<<“****************”<<endl; cout<<“****************”<<endl; void Print4lines( ) cout<<“****************”<<endl; cout<<“****************”<<endl; cout<<“****************”<<endl; cout<<“****************”<<endl; Flow of Control in Function Calls

  8. 2. Function Parameters parameter declaration void Print2lines( ) void Printlines(int numlines) cout<<“****************”<<endl; int count; cout<<“****************”<<endl; count=1; void Print4lines( ) while ( count <= numlines ) cout<<“****************”<<endl; cout<<“****************”<<endl; cout<<“****************”<<endl; cout<<“****************”<<endl; count++; cout<<“****************”<<endl;

  9. parameter #include <iostream> using namespace std; void Printlines(int numlines) void Printlines (int); int count; int main( ) argument count=1; while ( count <= numlines ) Printlines(2); cout<<“****************”<<endl; cout<<“Welcome Home!”<<endl; count++; Printlines(4); return 0;

  10. A variable or expression listed in a call to a function; also called actual argument or actual parameter. Argument A variable declared in a function heading; also called formal argument or formal parameter. Parameter

  11. 3.Syntax and Semantics of Void Functions include <iostream> FunctionPrototype using namespace std; void Printlines (int); void FunctionName(ParameterList); int main( ) FunctionCall Printlines(2); FunctionName(ArgumentList); ●●● Printlines(4); return 0; FunctionDefinition void FunctionName(ParameterList) void Printlines(int numlines) Statement ● ● ● ●●●

  12. FunctionDefinition void FunctionName(ParameterList) Statement ● ● ● 4. Parameters eg. value parameters void change(int p1, int p2) ● ● ● ParameterList reference parameters Datatype & VariableName , void change(int& p1, Datatype& VariableName , int& p2) ● ● ● ● ● ●

  13. Value parameter A parameter that receives a copy of the value of the corresponding argument. Because value parameters are passed copies of their arguments, anything that has a value may be passed to a value parameter. This includes constants, variables, and even arbitrarily complicated expressions. Printlines(3); Printlines(lineCount); Printlines(2*abs(10-someInt);

  14. There must be the same number of arguments in a function call as there are parameters in the function heading. Also, each argument should have the same data type as the parameter in the same position. Function heading: Void ShowMatch(float num1, int num2, char letter) Function call: ShowMatch(floatVariable, intVariable, charVariable ); implicit matching

  15. Reference parameter A parameter that receives the location ( memory address ) of the caller’s argument. Only a variable can be passed as an argument to a reference parameter because a function can assign a new value to the argument. void DoThis ( float val, int& count ) DoThis(someFloat , someInt); √ DoThis(9.83 , intCounter); DoThis(4.9*sqrt(y) , myInt); × DoThis( y , 3);

  16. & void Example (int& param1,//A reference parameter int param2,//A value parameter float param3)//Another value parameter

  17. eg. Please input the value of a andb at the keyboard. Put the big one in a and then output them (a and b).

  18. include <iostream> using namespace std; void change(int,int); int main( ) int a,b; cin>>a>>b; if ( a<b ) change(a,b); cout<<a<<endl; cout<<b<<endl; return 0; void change(int p1, int p2) int p; 5 p=p1; p1=p2; 9 p2=p; a b 5 9 p 5 p1 p2 5 9

  19. include <iostream> using namespace std; void change(int&,int&); int main( ) 9 5 int a,b; cin>>a>>b; if ( a<b ) change(a,b); cout<<a<<endl; cout<<b<<endl; return 0; void change(int& p1, int& p2) int p; p=p1; p1=p2; p2=p; a b 5 9 p2 p1 p 5

  20. void change(int *p1, int *p2) Statement ● ● ● pointer parameter ( address parameter )

  21. a b include <iostream> using namespace std; void change(int *,int *); int main( ) 9 &a &b 5 int a,b; cin>>a>>b; if ( a<b ) change(&a,&b); cout<<a<<endl; cout<<b<<endl; return 0; void change(int *p1, int *p2) int p; p=*p1; *p1=*p2; *p2=p; 5 9 p2 p1 p 5

  22. void test (int& ,int); void main( ) { int m=12,n=18; cout<<“m="<<m<<' '<<“n="<<n<<endl; test (m, n); cout<<“m="<<m<<' '<<“n="<<n<<endl; } void test(int& a, int b) { cout<<"a="<<a<<' '<<"b="<<b<<endl; a=a+b; b=a+b; cout<<"a="<<a<<' '<<"b="<<b<<endl; } 12 18 30 18 12 18 48 30

  23. Key: m = 12 n = 18 a = 12 b = 18 a = 30 b = 48 m = 30 n = 18

  24. Passing Arrays as Arguments when an array is passed as an argument, its base address (the memory address of the first element of the array) is sent to the function. The function then knows where the caller’s actual array is located and can access any element of the array.

  25. eg. void ZerOut( float[ ],int); //Function prototype : int main() { float arr1[30]; float arr2[90]; : ZeroOut(arr1,30); ZeroOut(arr2,90); : } void ZeroOut( float arr[ ], int numElements ) { int i; for( i=0; i<numElements; i++ ) arr[i]=0.0; }

  26. You use the reserved word const in the declaration of the parameter. void Copy( int destination[ ], const int source[ ], int size ) { int i; for ( i=0; i<size; i++) destination [i] = source [i]; }

  27. It is a common mistake to pass an array elementto a function when passing the entire array was intended. float arr[30]; : ZeroOut( arr[30], 30); ×

  28. int f1(int a[ ],int n); void main() { int b[8]={1,2,3,1,1,1,1,2}; int m1=f1(b,8); cout<<"m1="<<m1<<endl; } int f1(int a[ ],int n) { int i,f=1; for (i=0;i < n ;i++) f=f*a[i]; return f; } Key: m1 = 12

  29. Datatype arrName[ ] Datatype * pointName; Int f1 ( int a[ ], int k) { : } Int f1 ( int *a, int k) { : }

More Related