Various user defined function 'templates' C and C++ program example

 

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: Demonstrating various form of the C/C++ functions used in C/C++ programming

To show: How to create and use various forms of the C and C++ functions in C and C++ programming

 

// a function skeleton example passing by values

#include <iostream>

using namespace std;

 

// function prototypes and their variations. Notice and remember these variations in terms of receive and return values

void FunctOne(void);

double FunctTwo();

int FunctThree(int);

void FunctFour(int);

 

// main program, main() also a function

void main(void)

{

cout<<"I'm in main(void)..."<<endl;

 

// function call, go to FunctOne without any argument

FunctOne();

cout<<"\nBack in main(void)..."<<endl;

 

// function call, go to FunctTwo() without any argument

double q = FunctTwo();

 

// display the returned value

cout<<"The returned value = "<<q<<endl;

 

// function call, go to FunctThree() with an argument

int y = 100;

int x = FunctThree(y);

cout<<"Back in main(void)..."<<endl;

 

// display the returned value

cout<<"Display the returned value = "<<x<<endl;

 

int r = 50;

FunctFour(r);

// return nothing or just omit this 'return;' statement

return;

}

 

void FunctOne()

{

// do nothing here just display the following text

cout<<"\nNow I'm in FunctOne()!..."<<endl;

cout<<"Receives nothing, return nothing..."<<endl;

// return to main, without any returned value

// return; optionally, can put this empty 'return;'

}

 

double FunctTwo()

{

// receive nothing but do some work here...

double p = 10.123;

cout<<"\nNow I'm in FunctTwo()!\nmay do some work here..."

<<"\nReceives nothing but return something"

<<"\nto the calling function..."<<endl;

// and return something

return p;

}

 

int FunctThree(int z)

{

// receive something, do some work and return the something

int a = z + 100;

cout<<"\nThen, in FunctThree()!..."<<endl;

cout<<"Receive something from calling function\ndo some work here and"

<<"\nreturn something to the calling function...\n"<<endl;

 

// then return to main, with return value

return a;

}

 

void FunctFour(int s)

{

// received something but return nothing

int r = s - 20;

 

cout<<"\nNow, in FunctFour()..."<<endl;

cout<<"Received something, but return nothing..."<<endl;

cout<<"The value processed = "<<r<<endl;

// return; optionally can put this empty 'return;'

}

 

Output example:

 

I'm in main(void)...

Now I'm in FunctOne()!...

Receives nothing, return nothing...

Back in main(void)...

Now I'm in FunctTwo()!

may do some work here...

Receives nothing but return something

to the calling function...

The returned value = 10.123

Then, in FunctThree()!...

Receive something from calling function

do some work here and

return something to the calling function...

Back in main(void)...

Display the returned value = 200

Now, in FunctFour()...

Received something, but return nothing...

The value processed = 30

Press any key to continue . . .

 

 

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