C++ provides two kinds of subprograms:
void functions
value-returning functions
Chapter 7 Functions
1. Functional Decomposition with Void Functions
2. Function Parameters
3. Syntax and Semantics of Void Functions
4. Parameters
5. Designing Functions
1. Functional Decomposition with Void Functions
Writing Modules as Void Functions
int main( )
cout<<“ok”;return 0;
void print( )
cout<<“ok”;
Main function Void function
void main( )
********************************
Welcome Home!
****************************************************************
Print two lines of asterisksPrint “Welcome Home!”Print four lines of asterisks
Print “****************”Print “****************”
Print “****************”Print “****************”Print “****************”Print “****************”
Functional Decomposition in Pseudocode
Main Lever 0
Lever 1Print 2 lines
Print 4 lines
Print two lines of asterisksPrint “Welcome Home!”Print four lines of asterisks
Print “****************”Print “****************”
Print “****************”Print “****************”Print “****************”Print “****************”
Main
Print 2 lines
Print 4 lines
int main( )
Print2lines( ); cout<<“Welcome Home!”<<endl;Print4lines( );
return 0;
void Print2lines( )
cout<<“****************”<<endl;cout<<“****************”<<endl;
cout<<“****************”<<endl;cout<<“****************”<<endl;cout<<“****************”<<endl;cout<<“****************”<<endl;
void Print4lines( )
int main( )
Print2lines( ); cout<<“Welcome Home!”<<endl;Print4lines( );
return 0;
void Print2lines( )
· · · · · ·
include <iostream>using namespace std;void Print2lines( );void Print4lines( );
void Print4lines( )
· · · · · ·
//Function prototypes
//Function call
//Function call
//Function heading
//Function heading
int main( )
Print2lines( ); cout<<“Welcome Home!”<<endl;Print4lines( );
return 0;
void Print2lines( )
void Print4lines( )
cout<<“****************”<<endl;cout<<“****************”<<endl;
cout<<“****************”<<endl;cout<<“****************”<<endl;cout<<“****************”<<endl;cout<<“****************”<<endl;
Flow of Control in Function Calls
2. Function Parameters
void Print2lines( )
cout<<“****************”<<endl;cout<<“****************”<<endl;
cout<<“****************”<<endl;cout<<“****************”<<endl;cout<<“****************”<<endl;cout<<“****************”<<endl;
void Print4lines( )
void Printlines(int numlines)
int count;
count=1;while ( count <= numlines )
cout<<“****************”<<endl;count++;
parameter declaration
#include <iostream>
using namespace std;
void Printlines (int);
int main( )
Printlines(2);cout<<“Welcome Home!”<<endl;
Printlines(4);return 0;
void Printlines(int numlines)
int count;
count=1;while ( count <= numlines )
cout<<“****************”<<endl;count++;
argument
parameter
A variable or expression listed in a call to a function; also called actual argument or actual parameter.
A variable declared in a function heading; also called formal argument or formal parameter.
Parameter
Argument
3.Syntax and Semantics of Void Functions
FunctionDefinition
void FunctionName(ParameterList)Statement
●
●
●
FunctionCallFunctionName(ArgumentList);
FunctionPrototypevoid FunctionName(ParameterList);
include <iostream>using namespace std;void Printlines (int);
int main( )
Printlines(2);
Printlines(4);return 0;
void Printlines(int numlines)●●●
●●●
FunctionDefinition
void FunctionName(ParameterList)Statement
●
●
●
4. Parameters
ParameterList
Datatype VariableName
Datatype& VariableName ,
& ,
eg.
void change(int p1,int p2)
●
●
●
●
● ●
void change(int& p1,int& p2)●
●
●
value parameters
reference parameters
Value parameter A parameter that receives a copy of the value of the corresponding argument.
Printlines(3); Printlines(lineCount); Printlines(2*abs(10-someInt);
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.
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
Reference parameter A parameter that receives the location ( memory address ) of the caller’s argument.
DoThis(someFloat , someInt); DoThis(9.83 , intCounter); DoThis(4.9*sqrt(y) , myInt);
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( y , 3);
√×
&void Example (int& param1, //A reference parameter
int param2, //A value parameter
float param3) //Another value parameter
Please input the value of a and b at the
keyboard. Put the big one in a and then output them (a and b).
eg.
int main( )
int a,b; cin>>a>>b;
if ( a<b ) change(a,b);
void change(int p1,
using namespace std;void change(int,int);
include <iostream>
cout<<a<<endl;cout<<b<<endl;return 0;
int p2) int p;
p=p1; p1=p2; p2=p;
a5
b9
p15
p29
p5
9 5
int main( )
int a,b; cin>>a>>b;
if ( a<b ) change(a,b);
void change(int& p1,
using namespace std;void change(int&,int&);
include <iostream>
cout<<a<<endl;cout<<b<<endl;return 0;
int& p2) int p;
p=p1; p1=p2; p2=p;
a5
b9
p1 p2p5
9 5
pointer parameter ( address parameter )
void change(int *p1, int *p2)
●
●
●
Statement
int main( )
int a,b; cin>>a>>b;
if ( a<b ) change(&a,&b);
void change(int *p1,
using namespace std;void change(int *,int *);
include <iostream>
cout<<a<<endl;cout<<b<<endl;return 0;
int *p2) int p;
p=*p1; *p1=*p2; *p2=p;
a5
b9
p1 p2p5
9 5
&a &b
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
30
12
30
18
18
18
48
m = 12 n = 18a = 12 b = 18a = 30 b = 48m = 30 n = 18
Key:
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.
eg.
void ZeroOut( float arr[ ], int numElements ){
int i;for( i=0; i<numElements; i++ )
arr[i]=0.0;}
void ZerOut( float[ ],int); //Function prototype : int main(){
float arr1[30]; float arr2[90]; : ZeroOut(arr1,30); ZeroOut(arr2,90); :}
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];}
It is a common mistake to pass an array element to a function when passing the entire array was intended.
float arr[30]; : ZeroOut( arr[30], 30); ×
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
Datatype arrName[ ]
Datatype * pointName;
Int f1 ( int a[ ], int k)
{
:
}
Int f1 ( int *a, int k)
{
:
}