By a constant argument, it means that the function cannot
modify the argument. If you pass a constant value to the function, then the
function cannot modify the value as the value is constant. For example see the
following sample program:
#include
<iostream.h>
#include
<string.h>
#include
<conio.h>
int len(const char[]);
int len(const char str[])
{
str[1]=str[1]+32;
return(strlen(str));
}
void main()
{
int k=0;
k=len("My
String");
cout<<"Length
of the string = "<<k;
getch();
}
On compiling the above program
you will get the error message as – “Cannot modify a
const object”. Because I am trying to modify the value of a
constant argument in the line str[1]=str[1]+32; ( Trying to add 32 with the character at the
index 1 of the character array str). Thus we have seen that const qualifier in function prototype and
so in definition, tell the compiler that the function should not modify the
argument. The constant arguments are useful when the functions are called by
reference.
Good. The only satisfactory answer I got from here
ReplyDeleteThe only satisfactory answer I got from here.very Good
ReplyDelete