Doing some arithmetic operations in demonstrating the C++ function that receive 2 floats and return a float

 

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: Doing some arithmetic operations in demonstrating the C++ function that receive 2 floats and return a float

To show: How to create and use a user defined function in C++ programming which receive floats and return a float

 

// user defined function and header file. A very simple arithmetic functions

#include <iostream>

using namespace std;

 

// function prototype

float AddNum(float, float);

 

void main(void)

{

// global (to this file) scope variables

float p, q, r;

 

// prompt for user input

cout<<"Enter two numbers separated by a space: "<<endl>>p>>q;

 

// function call

r = AddNum(p, q);

 

// display the result

cout<<"Addition: "<<p <<" + "<<q<<" = "<<r<<endl;

}

 

// function definition

float AddNum(float p, float q)

{

return (p + q);

}

 

Output example:

 

Enter two numbers separated by a space:

20 13

Addition: 20 + 13 = 33

Press any key to continue . . .

 

 

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