true/false about function parameter

is it true or false

a function like void myfun(int num){} can receive type "int var" but can't receive type "const int var"

AND

a function like void myfun(const int num){} can receive both type "int var" and also type "const int var"
Yes, it is true.
Last edited on
False and True.

The const-correctness on the parameters passed to a function only specifies whether the function can modify them. In your case, the first function's num can be modified, but the second one can't.

However, you aren't passing the original value, but rather a copy of it. Hence, it doesn't matter if you pass it a constant or a normal value, it will still be the same. The only time it matters is if you are passing a pointer or a reference.

As an aside, the second function signature will typically never be used. The contents of a function is an implementation detail, and due to the const serving no purpose to anything but the implementation it is just obstructing and confusing to people reading the function's signature to determine it's purpose.
Last edited on
Top-level const qualifiers on function parameters are ignored on function declarations and have no bearing on how the function is called.

void myfun(int num); and void myfun(const int num); declare the same function whose type is void(int), that is taking a non-const int by value and returning void.

void myfun(int num){} and void myfun(const int num){} are two different definitions of the same function (and so you can't have both in a program)

(personally, I strip const in the headers since it's not part of interface, but keep it in the definitions if it makes sense)
Last edited on
Topic archived. No new replies allowed.