Why do we have const arguments?

S

Sathyaish

Why do we declare some arguments as constant? Is it because we do not
expect the value of those constant arguments to change in the
function?
 
E

E. Robert Tisdale

Sathyaish said:
Why do we declare some arguments as constant? Is it because
we do not expect the value of those constant arguments to change
in the function?

More precisely, we want to make sure that
they are not [accidentally] changed in the function.

int f(const int i) {
i = 13; // error
return i + 1;
}

Your compiler should complain about this.

const
char* car(const char* s) {
return ('\0' == *s)? s: s + 1;
}

In this case, we want to prevent the function
from changing any of the characters in the string
which begins with the character to which s points.
 
M

Martin Dickopp

Why do we declare some arguments as constant?

First of all, I presume you mean "parameters" instead of "arguments".
Arguments are expressions used in a function call, their value is
assigned to the parameters. In this example

int foo (int i) { return i + 1; }
int main (void) { foo (42); return 0; }

`i' is a parameter of the `foo' function; the latter is called with an
argument of `42'.

Now to your question: Parameters are declared with a const qualifier for
the same reason that other block-scope variables are declared as const,
to indicate (to both the compiler and the human reader) that their value
doesn't change.
Is it because we do not expect the value of those constant arguments
to change in the function?

Yes.

Martin
 
M

Martin Dickopp

E. Robert Tisdale said:
Sathyaish said:
Why do we declare some arguments as constant? Is it because we do not
expect the value of those constant arguments to change in the function?

More precisely, we want to make sure that
they are not [accidentally] changed in the function.

int f(const int i) {
i = 13; // error
return i + 1;
}

Your compiler should complain about this.

const
char* car(const char* s) {
return ('\0' == *s)? s: s + 1;
}

In this case, we want to prevent the function
from changing any of the characters in the string
which begins with the character to which s points.

Note that in the latter example, the parameter `s' is /NOT/ const
qualified, although the function doesn't change its value, so it would
have been valid to const qualify it (`const char *const s').

Martin
 
D

Darrell Grainger

Why do we declare some arguments as constant? Is it because we do not
expect the value of those constant arguments to change in the
function?

I see there have been some good responses to your question. I'd just like
to add that a const modifier is helpful when the input parameter is a
pointer. By passing in a pointer I can change the contents of what it
points to. If I use a const modifier it tells the compiler (and any
programmer using my function) that the contents to which the pointer
references will not be altered. For example,

char *strcpy(char *s1, const char *s2);

I immediately know that the second string will not be altered. It also
tells me, a user of strcpy, that the first parameter is the destination
and the second parameter is the source.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top