![]() ![]() |
![]() |
|||||
![]()
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]()
|
CSET 3150 C++ Functions More About FunctionsScopeScope refers to the part of the program in which a declaration is visible. In C++, there are three kinds of scope: block, function and file. Block and function scope are also known as local scope. File scope is also known as global scope. The scope for an identifier is determined by the location of the identifier's declaration. The scope of local identifiers is limited to the block or function in which they are declared. We can also declare global variables that are visible from any point of the program, inside and outside all functions. In order to declare global variables you simply place the declaration outside of any function or block. Global declarations are usually placed immediately after the compiler directives at the top of the program. The figure below illustrates declaration scope for several variable identifiers that are part of the C++ program example.cpp.
Here is an explanation of the scope for each variable identifier:
C++ allows variables of different scope to have the same identifier. However, it is recommended that you choose unique identifiers - independent of scope. Choosing identical identifiers can lead to confusion. Pass by ValueThe normal function parameters are conatants or expressions that are evaluated before the function is called. The resulting value is the initial value of the variable listed in the functiuon header. For example, given the function definition below:
float FindAreaRect ( float length, float width )
{
float area;
area = length * width;
return (area);
}
we could use the following call-by-value calls to the FindAreaRect() function from main():
A = FindAreaRect ( 24.5, 5.3 ); Area = FindAreaRect ( sideOne, sideTwo ); Lets look at the first of these calls: A = FindAreaRect ( 24.5, 5.3 );. When the function is called as part of the assignment statement, the parameter values are passed to the function. The function then assigns these values to its "receiving" parameter identifiers (e.g., sideOne = 24.5 and sideTwo = 5.3) and proceeds to calculate a value for area.
The value of area is then returned to the calling statement in main() and assigned to the variable identifier A.
Now we can look at the second call: Area = FindAreaRect ( sideOne, sideTwo );.
In all the functions we have seen so far, the parameters passed to the functions have been passed by value. This means that when calling a function with parameters, what we have passed to the function were copies of their values but never the variables themselves. This "protects" the original variable values. That is, the values of sideOne and sideTwo remain unchanged. This would remain true, even if the FindAreaRect() function were written as follows:
float FindAreaRect ( float length, float width )
{
float area;
area = length * width;
length = 0;
width = 0;
return (area);
}
Pass by ReferenceCall by reference is a way of passing arguments (or 'parameters') to functions. Instead of passing the value itself, a reference to the value is passed. As a consequence, the value can be modified from within the subroutine. Also, if values can be large, it is significantly cheaper than call by value. Default ParametersWhen declaring a function we can specify a default value for each parameter. This value will be used if the corresponding argument is left blank when calling to the function. To do that, we simply have to use the assignment operator and a value for the arguments in the function declaration. If a value for that parameter is not passed when the function is called, the default value is used, but if a value is specified this default value is ignored and the passed value is used instead. Function OverloadingFunction overloading is the process of using the same identifier for two or more functions. However, each function will be different in terms of either different types of parameters (e.g., int, double, etc.) or a different number of parameters. These differences allow the compiler to select which function to call in any given situation. Function overloading is one of the most powerful features of C++ programming language. It forms the basis of polymorphism. Here is a simple example of function overloading. The cube function in this example is overloaded by parameter type. The compiler knows the correct version of cube to call based on the type of the parameter passed; in this example four different data types are passed.
// ovldFun_1.cpp
#include<iostream>
#include<string>
using namespace std;
int cube(int);
long cube(long);
double cube(double);
string cube(string);
int main()
{
int num1 = 2;
long num2 = 3;
double num3 = 5.0;
string s1 = "cat";
cout << cube( num1 ) << "\n"
<< cube( num2 ) << "\n"
<< cube( num3 ) << "\n"
<< cube( s1 ) << endl;
return (0);
}
int cube( int i )
{
return (i * i * i);
}
long cube( long l )
{
return (l * l * l);
}
double cube( double d )
{
return (d * d * d);
}
string cube( string s1 )
{
return (s1 + s1 + s1);
}
et791:~/cset3150$ ./ovldFun This example illustrates overloading functions with respect to the number of parameters passed.
// ovldFun_2.cpp
#include<iostream>
using namespace std;
int sum(int, int);
int sum(int, int, int);
int sum(int, int, int, int);
int sum(int, int, int, int, int);
int main()
{
cout << "4+5 = " << sum( 4, 5 ) << "\n"
<< "8+10+3 = " << sum( 8, 10, 3 ) << "\n"
<< "17+1+22+11 = " << sum( 17, 1, 22, 11 ) << "\n"
<< "2+9+55+12+6 = " << sum( 2, 9, 55, 12, 6 ) << endl;
return (0);
}
int sum( int i1, int i2 )
{
return (i1 + i2);
}
int sum( int j1, int j2, int j3 )
{
return (j1 + j2 + j3);
}
int sum( int k1, int k2, int k3, int k4 )
{
return (k1 + k2 + k3 + k4);
}
int sum( int l1, int l2, int l3, int l4, int l5 )
{
return (l1 + l2 + l3 + l4 + l5);
}
et791:~/cset3150$ ./ovldFun_2 Function TemplatesFunction templates provide a functional behavior that can be called for different types. In other words, a function template represents a family of functions. The representation looks a lot like an ordinary function, except that some elements of the function are left undetermined: These elements are parameterized. The following is a function template that returns the maximum of two values:
// max.h
template <typename T>
inline T const& max (T const& a, T const& b)
{
if a < b
{
return(b);
}
else
{
return (a);
}
}
This is a shorter version of the same template.
// max.h
template <typename T>
inline T const& max (T const& a, T const& b)
{
// if a < b then use b else use a
return a<b?b:a;
}
This template definition specifies a family of functions that returns the maximum of two values,
which are passed as function parameters a and b.
The type of these parameters is left open as template parameter T.
// max.cpp
#include <iostream>
#include <string>
#include "max.h"
using namespace std;
int main()
{
int i = 42;
cout << "max(7,i): " << ::max(7,i) << endl;
double f1 = 3.4;
double f2 = -6.7;
cout << "max(f1,f2): " << ::max(f1,f2) << endl;
string s1 = "mathematics";
string s2 = "math";
cout << "max(s1,s2): " << ::max(s1,s2) << endl;
return (0);
}
et791:~/cset3150$ ./funcTemplate GlossaryThe terms below are used in the lesson above. These terms are defined in the context of their use with respect to C++ functions.
|
|||||
|
|
|
Added to the Web: September 10, 2007. Web page design by Dan Solarek. |
![]() http://cset.sp.utoledo.edu/cset3150/ |