Chapter 4 Quick Reference


To

Do this

Declare a function prototype.

Specify the return type of the function, followed by the function name, followed by the parameter list enclosed in parentheses. Remember the semicolon at the end of the function prototype. For example:

 double MyFunction(int p1, short p2);

Define default parameters.

Define default parameters in the function prototype, if required. Use an = operator, followed by the default value. For example:

 double MyFunction(int p1, short p2=100);

Define a function body.

Specify the return type of the function, followed by the function name, followed by the parameter list enclosed in parentheses. Do not specify default parameters here. Define the function body within braces. For example:

 double MyFunction(int p1, short p2) { int n = p1 + p2; ... }

Return a value from a function.

Use the return keyword, followed by the value you want to return. For example:

 return (p1 + p2) / 2.00;

Call a function.

Specify the function name, and pass parameter values within parentheses. If the function returns a value, you can assign it to a variable. For example:

 double result = MyFunction(100, 175);

Define and use
global variables.

Define the global variable outside of any function. Use the variable in any subsequent function in the source file. For example:

 int myGlobal = 0; void MyFunction() { myGlobal++; ... }

Define and use overloaded functions.

Define several functions with the same name but different parameter lists. Implement each function. Call the version you want, using appropriate parameter values. For example:

 // Prototypes void MyFunction(int p1); void MyFunction(double p1, double p2); ... // Function calls MyFunction(100); MyFunction(2.5, 7.5); ... // Function bodies void MyFunction(int p1) { ... } void MyFunction(double p1, double p2) { ... }




Microsoft Visual C++  .NET(c) Step by Step
Microsoft Visual C++ .NET(c) Step by Step
ISBN: 735615675
EAN: N/A
Year: 2003
Pages: 208

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net