Declaring, defining and using user defined function in C++ programming

 

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: Some arithmetic operations to demonstrate the custom made function in C++ programming

To show: How to declare, define and used the user defined function in C++ programming

 

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

#include <iostream>

using namespace std;

 

// function prototype, receive 2 floats and return a float

float AddNum(float, float);

 

// main program

void main(void)

{

cout<<"In main(void), this just a program skeleton..."<<endl;

 

// function call

float x = AddNum(2.5, 3.5);

}

 

// function definition

float AddNum(float , float)

{

// we just use the local

float x = 10;

 

cout<<"The function body..."<<endl;

cout<<"In AddNum(), x = "<<x<<"\n";

 

return x;

}

 

Output example:

 

In main(void), this just a program skeleton...

The function body...

In AddNum(), x = 10

Press any key to continue . . .

 

 

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