The C++ user defined function code sample demonstrating passing and processing the function argument

 

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: The C++ user defined function code sample demonstrating passing and processing the function argument

To show: How to create and use a simple user defined function in C++ programming

 

#include <iostream>

using namespace std;

 

// function prototype

void prt(int);

 

// the main function definition

int main(void)

{

// declare an integer variable, local to main()

int x = 12;

// calls prt() and passes x

cout<<"\nIn main(void) before function call, x = "<<x<<endl;

 

prt(x);

 

cout<<"\nIn main(void) after function call, x = "<<x<<endl;

cout<<endl;

 

return 0;

}

 

// the prt() function definition

void prt(int y)

{

// local variable to prt()

int p = 30;

 

// value from calling program

cout<<"In prt(), value from the calling program..."<<endl;

cout<<"x's value = "<<y<<endl;

 

// local variable value

cout<<"\nIn prt(), local variable value..."<<endl;

cout<<"p's value = "<<p<<endl;

}

 

Output example:

 

In main(void) before function call, x = 12

In prt(), value from the calling program...

x's value = 12

In prt(), local variable value...

p's value = 30

In main(void) after function call, x = 12

Press any key to continue . . .

 

 

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