answersLogoWhite

0


Best Answer

Constants are simply variables that do not change value. This is particularly useful when we need to continually refer to the same value, such as the length of a fixed-length array:

const int array_max = 100;

int array[array_max];

for (int i=0; i<array_max; ++i) {

array[i] = i * i;

}

// ...

Using a constant in this way reduces the maintenance burden considerably. For example, suppose we later decide that we really needed 200 integers in our array instead of just 100. So long as our code uses the constant, array_max, we need only change the constant's value and that change will be reflected wherever the constant is used. If we has used the literal constant, 100, instead, we'd need to search through our code and update each usage of that literal. That can lead to human errors and inconsistency in our code.

All constants are allocated in the static segment (where all static variables are allocated) even when they are declare locally. The one exception is a constant formal argument:

void f (const int i) {

// ...

}

Arguments are always passed by value, so i will hold a copy of whichever value was passed to it. Being a copy, any changes made to i will not be reflected in the calling code, however if the function must not modify the value, declaring the formal argument constant ensures we don't inadvertently change the value.

Typically we use constant formal arguments when those arguments are pointers or references rather than values. When we pass an object by reference or by pointer, we are passing a copy of the object's address and this would allow the function to indirectly modify the object. This is known as a side-effect and is undesirable. To give assurance to the caller that the object will not be changed, we use a constant reference or pointer:

void f (const obj& x) {

// ...

}

If we actually want the function to modify the object, the best method of doing so is to return a new object by value and leave the argument constant:

obj f (const obj& x) {

// ...

}

In this way the caller can decide what to do with the returned object:

obj y;

obj z = f (y); // create a new object from y without changing y

y = f (y); // modify y

f (z); // ignore the modification completely

Note that returning objects by value is normally expensive as the object must be copied. However, objects that implement the move semantic can be returned extremely efficiently, particularly those objects that are really just resource handles. This includes all the standard library containers such as std::vector and std::list. Moving a vector is many times quicker than copying one because all we're really doing is changing ownership of the resource; and that's (almost) as efficient as copying a pointer and assigning NULL to the original.

User Avatar

Wiki User

6y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

12y ago

Unlike a variable, which is expected to change its value from time to time, a constant is a value that we do not expect to change.

When passing references to functions, it's always reassuring to see the reference parameter has been declared const, as this assures us the object's immutable members will not be altered by the function. If the parameter is variable (non-const), we can expect the object to be altered. If we don't want the object altered, we must pass a copy of our object instead.

Copy constructors always use a constant reference. This makes sense since we're only every interested in copying the object's members, not to alter them.

class cMyClass

{

public: // copy constructor...cMyClass( const cMyClass & myClass):m_Data( myClass.m_Data ){}

private: int m_Data;

};

We can also declare values to be constants that are local to a class, or local to a function. For instance, in a playing-card simulation, we know in advance that, in any one deck of cards, there can only ever be 52 playing cards. Since this is highly unlikely to change for the duration the program is running, we can declare a global constant that we can use wherever we would have otherwise used the literal value 52.

static const int MAX_CARDS = 52;

The use of upper-case is a visual aid -- to remind us it is a global constant, not a variable.

This answer is:
User Avatar

User Avatar

Wiki User

12y ago

Yes, you can. But output of program employing constant functions might surprise you. The best way is to experiment and see how it works.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is the function of constants in c plus plus?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

What is a linear function?

A function of the form f(x) = mx + c where m and c are constants is linear.


What are functions in c language?

constants, MAX_(function), etc.


How can you tell if a function is linear or non linear?

If it can be written in the form y = mx + c where m and c are constants [or, equivalently, ax + by = k where a, b and k are constants] then y is a linear function of x.


How do you determine that a function is quadratic function?

It can be written in the form y = ax2 + bx + c where a, b and c are constants and a &acirc;&permil;&nbsp; 0


What is a standard form of a quadratic function?

ax2 + bx + c = 0 where a, b and c are constants and a is not 0.


What are the building function in c plus plus?

There is no such term as "building function" in C++.


Coding constants in c?

Coding constants in c means writing the constants in a certain way that the c language understands.


What is exponential function?

"The" exponential function is ex. A more general exponential function is any function of the form AeBx, for any non-xero constants "A" and "B". Alternately, Any function of the form CDx (for constants "C" and "D") would also be considered an exponential function. You can change from one form to the other.


What is the quadratic function written as in standard from?

The quadratic equation, in its standard form is: ax2 + bx + c = 0 where a, b and c are constants and a is not zero.


What is a function of the form y equals mx plus b where m and b are constants?

It is the equation of a straight line


A c plus plus statement that invokes a function is known as?

...a function call.


When will you make a function inline in c plus plus?

yes,we can make function inline