<< Chapter < Page Chapter >> Page >

double side;

cin>>side;

cout<<"Volume of cube with side "

<<side<<" is "<<cube(side)<<endl;

return 0;

}

The output of the above program:

Enter the side length of your cube: 3.5

Volume of cube with side 3.5 is 42.875

Function overloading

C++ enables several functions of the same name to be defined, as long as these functions have different sets of parameters (at least their types are different). This capability is called function overloading .

When an overloaded function is called, the C++ compiler selects the proper functions by examining the number, types and order of the arguments in the call.

Function overloading is commonly used to create several functions of the same name that perform similar tasks but on different data types.

Example:

void showabs(int x)

{

if( x<0)

x = -x;

cout<<“The absolute value of the integer is “<<x<<endl;

}

void showabs(double x)

{

if( x<0)

x = -x;

cout<<“The absolute value of the double is “<<x<<endl;

}

The function call

showabs(10);

causes the compiler to use the first version of the function showabs.

The function call

showabs(6.28);

causes the compiler to use the second version of the function showabs.

Default arguments

C++ allows default arguments in a function call. Default argument values are listed in the function prototype and are automatically transmitted to the called function when the corresponding arguments are omitted from the function call.

Example: The function prototype

void example (int, int = 5, float = 6.78);

provides default values for the two last arguments.

If any of these arguments are omitted when the function is actually called, the C++ compiler supplies these default values.

Thus, all following function calls are valid:

example(7, 2, 9.3); // no default used

example(7, 2); // same as example(7, 2, 6.78)

example(7); // same as example(7, 5, 6.78)

Variable scope

Scope refers to where in your program a declared variable or constant is allowed used. A variable can be used only when inside its scope.

Global scope refers to variables declared outside of any functions or classes and that are available to all parts of your program.

Local scope refers to a variable declared inside a function and that is available only within the function in which it is declared.

Example:

#include<iostream.h>

int x; // create a global variable named firstnum

void valfun(); // function prototype (declaration)

int main()

{

int y; // create a local variable named secnum

x = 10; // store a value into the global variable

y = 20; // store a value into the local variable

cout<<"From main(): x = "<<x<<endl;

cout<<"From main(): y = "<<y<<endl;

valfun(); // call the function valfun

cout<<"\nFrom main() again: x = "<<x<<endl;

cout<<"From main() again: y = "<<y<<endl;

return 0;

}

void valfun() // no values are passed to this function

{

int y; // create a second local variable named y

y = 30; // this only affects this local variable's value

cout<<"\nFrom valfun(): x = "<<x<<endl;

cout<<"\nFrom valfun(): y = "<<y<<endl;

x = 40; // this changes x for both functions

return;

}

The output of the above program:

From main(): x = 10

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Programming fundamentals in c++. OpenStax CNX. Jul 29, 2009 Download for free at http://cnx.org/content/col10788/1.1
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Programming fundamentals in c++' conversation and receive update notifications?

Ask