A simple mathematical operation to demonstrate the C++ function that receive a float and return nothing

 

Compiler: Visual C++ Express Edition 2005

Compiled on Platform: Windows XP Pro SP2

Header file: Standard

Additional project setting: Set project to be compiled as C++

Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C++ Code (/TP)

Other info: none

To do: A simple mathematical operation to demonstrate the C++ function that receive a float and return nothing

To show: How to declare, define and call C++ function that receive a float and return nothing

 

// program showing a function of type void

#include <iostream>

using namespace std;

 

int main(void)

{

float y1, y2, avgy;

 

// function prototype, display-avg() is declared to be of type void receive a float and return void (nothing)

void display_avg(float);

y1 = 5.0;

y2 = 7.0;

 

cout<<"\ny1 = "<<y1<<"\ny2 = "<<y2;

avgy = (y1 + y2)/2; // compute average

 

display_avg(avgy); // call function display_avg()

cout<<endl;

 

// return to the environment/system

return 0;

}

 

// display_avg() receive a float and return nothing

void display_avg(float avgx)

{

// just display the received value

cout<<"\nThe average is = "<<avgx;

return;

// no value is returned to main(void) and control reverts to main() or just excludes the return word

}

 

Output example:

 

y1 = 5

y2 = 7

The average is = 6

Press any key to continue . . .

 

 

C and C++ Programming Resources | C & C++ Code Example Index