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

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

description

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. - PowerPoint PPT Presentation

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

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

C++ provides two kinds of subprograms:

void functions

value-returning functions

Page 2: 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

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

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

********************************

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

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

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( )

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

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

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

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

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

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

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

#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

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

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

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

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)●●●

●●●

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

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

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

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.

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

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

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

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);

√×

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

&void Example (int& param1, //A reference parameter

int param2, //A value parameter

float param3) //Another value parameter

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

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.

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

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

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

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

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

pointer parameter ( address parameter )

void change(int *p1, int *p2)

Statement

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

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

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

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

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

m = 12 n = 18a = 12 b = 18a = 30 b = 48m = 30 n = 18

Key:

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

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.

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

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); :}

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

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];}

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

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); ×

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

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

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

Datatype arrName[ ]

Datatype * pointName;

Int f1 ( int a[ ], int k)

{

:

}

Int f1 ( int *a, int k)

{

:

}