Computernerd
Computernerd

Reputation: 7766

Function Prototype

My teacher said that [A] was the correct ans but why isnt it [C]. whats wrong with option [B] as a prototype. Option [B] looks perfectly fine to me

Which of the following function prototype is perfectly acceptable?

[A]. int Function(int Tmp = Show());

[B]. float Function(int Tmp = Show(int, float));

[C]. Both A and B.

[D]. float = Show(int, float) Function(Tmp);

Upvotes: 1

Views: 1862

Answers (1)

Joseph Mansfield
Joseph Mansfield

Reputation: 110658

float Function(int Tmp = Show(int, float));

This defines a function called Function that returns a float and takes one argument that is an int called Tmp. The Tmp is being given a default value, but the default value is the part that is invalid. What value is Show(int, float)? It appears to be wanting to call a function called Show (or construct a temporary object of type Show), passing int and float as arguments. But int and float are not valid arguments to a function.

In fact, there are only a few places I can think of Show(int, float) being a possible production of the C++ grammar (without using the preprocessor). First is as part of a function declaration for Show. For example:

int Show(int, float);

Second is, if Show is a type, giving a function type. For example in:

foo<Show(int,float)>();

There are probably a few other similar cases, none of which actually call a function called Show.

Upvotes: 6

Related Questions