C++. Passing a constant parameters to a function. The return of constant from function




Passing a constant parameters to a function. The return of constant from function


Contents


Search other websites:

1. In what cases when passing parameter to a function it must be declares as constant?

The constant parameter received by the function can not be changed in the body of the function. This means you can not use an assignment operation in which a constant parameter receives a value. However, the value of constant parameter can be used at the right hand of assignment statement.

A constant parameter is declared in the case when it is necessary that the value of the transferred object remains unchanged in the body of the called function. This case is possible when the argument’s value is passed by the address when function is called. To pass by address a pointer or a reference to variable is used.

If the argument is passed by value, then there is no sense to declare a parameter with the keyword ‘const’. Since, in this case, the function gets a copy of the original variable. Changing a copy of a variable will not cause this variable to change in the program.

For more details about passing parameters to a function by value and address is described in the topic:






2. How is declared passing a constant parameter to a function?

To pass a constant parameter to a function, you must use the ‘const’ keyword before declaring the parameter. The general form of the constant parameter:

const type parameter

where

  • type – type of constant parameter;
  • parameter1 – name of constant parameter.

The general form of a function declaration that receives N constant parameters:

return_type FunName(const type1 parameter1, const type2 parameter2, ..., const typeN parameterN)
{
    ...
}

where

  • FunName – the name of function;
  • return_type – type that function returns;
  • type1, type2, …, typeN – types of constant parameters with names parameter1, parameter2, …, parameterN;
  • parameter1, parameter2, …, parameterN – names of constant parameters.

3. Example of declaring and using a function that receives a constant parameter. Passing parameter by value

In the example Area() function is declared, that receives a constant parameter ‘radius’.

// a function that calculates the area of a circle over a given radius
// the value of 'radius' is passed as a constant
double Area(const double radius)
{
    return double (3.1415 * radius * radius);
}

Using a function in another program code

// using a function Area()
double area;
area = Area(5.5); // area = 95.0304

// so too can
double r = 7.88;
area = Area(r); // area = 195.07

If you try to change the value of the radius constant in the Area() function, for example

radius = 2.88;

the compiler will generate an error:

'radius': you cannot assign to a variable that is const.

4. Example of declaring and using a function that receives a constant parameter. Passing parameter by pointer

The Count() function, which counts the number of characters in the string is declared. The string is constant parameter

// A function that counts the number of characters specified in an ASCIIZ string
// string s is passed as a constant parameter
int Count(const char * s, char sym)
{
    int i=0, k=0;
    while (s[i] != '\0')
    {
        if (s[i] == sym) k++;
        i++;
    }
    return k;
}

Using the Count() function

// using the Count() function
int n;
char * str1 = "Hello world!";
char * str2 = "bestprog.net";

n = Count(str1, 'o'); // n = 2
n = Count(str1, 'l'); // n = 3
n = Count(str2, 't'); // n = 2

5. Example of declaring and using a function that receives a constant array of integers

Sometimes you need to protect yourself from accidentally changing the values of the array elements. In this case, it is advisable to declare the array as constant. The following is the function CountZero(), which counts the number of zero elements of the array

// function that counts the number of zero elements of array A
// array A is set as a constant
int CountZero(const int * A, int n)
{
    int i=0, k=0;
    for (i=0; i<n; i++)
        if (A[i]==0) k++;
    return k;
}

If in the body of the CountZero() function, you’l try change the values of the array element A, for example

A[3] = 15;

the compiler will generate an error.

Using the CountZero() function in another program code

// use the CountZero() function
int n = 10;
int AA[] = { 3, 5, 6, 0, 0, -2, 1, -2, 0, 7 };
n = CountZero(AA, n); // n = 3

6. Examples of function declarations that return a constant using the operator return

It is possible to declare a function that returns a constant.

Example 1. A function that returns a constant number of type double.

// function that returns a constant number
const double Pi(void)
{
    return 3.141592;
}

Using the Pi() function in another code

// using the Pi() function
double d;
d = Pi(); // d = 3.141592

Example 2. A function that returns a constant string.

// a function that returns a constant string
const string GetSiteName(void)
{
    return "bestprog.net";
}

Using the GetSiteName() function in another code

// using the GetSiteName() function
string s;
s = GetSiteName(); // s = "bestprog.net"


Related topics