Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

So while I've been doing some lengthy compiles I decided to take the C++ general test on ODesk and came across this question.

This question

If I'm not mistaken, given the wording (or lack thereof) all of these could be true.

a.

int Foo() { }
int Foo(int bar) { }

b. Well, "return void" would be incorrect semantically but functions can obviously have void return types.

void Foo() { }

c. This is the definition of inline functions, yes.

d. Without going into much detail about the placement of the following elements,

typedef void (*Func)(int);

Func functions[2];

void Foo(int bar) { }
void Bar(int foo) { }

functions[0] = &Foo;
functions[1] = &Bar;

Further, you could always do this using lambdas and functors.

e.

void Foo(int& bar)
{
    ++bar;
}

int foobar = 5;
Foo(foobar);

f.

int bar = 5;

int& GetBar()
{
    return bar;
}

GetBar() = 6;

g.

int bar = 5;

int* GetBar()
{
    return &bar;
}

(*GetBar()) = 5;

I fail to see where this question has any truly false answers. Am I missing something?

Needless to say I ran out of time and failed the whole thing. I guess I'm a bad C++ programmer. :(

share|improve this question
14  
Came here to yell at some kid pasting homework, was pleasantly surprised. –  SomeKittens Apr 18 at 0:27
    
Actually not all languages require that inline functions are expanded. "inline" is a compiler hint rather than a command in Delphi, and C, C++ have exceptions. So "c" can be false as well. It might be more entertaining to write an answer showing how each statement can be wrong. –  Ӎσᶎ Apr 18 at 1:44
1  
@Ӎσᶎ If only there was a text box. I want to go back and hit the "Report an issue with this question". –  Qix Apr 18 at 3:06
5  
That is not an array of functions. That is an array of function pointers. –  immibis Apr 18 at 8:45
    
@immibis True, but the question doesn't really clarify (at least to me it didn't). –  Qix Apr 18 at 16:30

4 Answers 4

up vote 18 down vote accepted

This whole question is dodgy. The question statement implies the possibility of multiple choices, while the radio buttons indicates a single choice.

Furthermore, b is pretty suspect, as void functions don't return anything.

D is also questionable, for as far as I know, you cannot have an array of functions. Sure, you can have an array of function pointers, but thats not exactly the same thing.

share|improve this answer
    
Right; D is like asking if you can call an int. Sure, if you cast it to a pointer and that pointer refers to a function... –  Qix Apr 18 at 0:15
1  
I'm willing to bet D is the answer they are going for, but yes, the question is poor. –  Steven Burnap Apr 18 at 5:30
    
While void functions don't return any value, they still return the type void. Thus I say B is true. –  Thomas Eding Apr 22 at 22:37
    
@ThomasEding That's still really bad wording on their part. You don't return void;, but instead return;. –  Qix Apr 24 at 16:14

c. This is the definition of inline functions, yes.

That is not completely right.
Despite its name, the keyword inline does not guarantee that a function will be inlined. The only thing that you can be sure of is that the compiler will not complain if it sees the definition of an inline function multiple times.

So, strictly speaking, option C is incorrect, but I don't think it is the answer that the authors of the quiz expect, because there is an answer that is even more wrong.

d. Without going into much detail about the placement of the following elements,

typedef void (*Func)(int);

Func functions[2];

void Foo(int bar) { }
void Bar(int foo) { }

functions[0] = &Foo;
functions[1] = &Bar;

Further, you could always do this using lambdas and functors.

In your example, functions is an array of pointers to functions and not an array of functions themselves. Similarly, lambdas and functors are not really functions either. They are classes that respond to the function-call operator, but that doesn't make them functions in the eyes of the C++ language definition.

Functions in C++ are a bit like second-class citizens. You can define and call them, but as soon as you try to do something else with a function type (like create an array of them), you either get an error or they silently convert themselves in a pointer.

share|improve this answer
    
I agree with the D bit, but I was reading c as functions that have already been positively marked for inlining, versus the inline keyword itself. I understand inline is a hint. –  Qix Jun 4 at 16:50

Answer c) is also arguably false.

"Inline functions are expanded during compile time to avoid invocation overhead."

Firstly, the C++ compiler is permitted to ignore the inline hint and not expand the functions at all.

Secondly, the stated "reason" for doing expansion is an oversimplification. In fact, the real reason for doing the expansion (or not) is entirely in the hands of the compiler writer.

share|improve this answer
    
You're correct, but I was thinking about it in the sense of 'okay, the compiler has determined it does want to inline a function' rather than the inline hint. –  Qix Apr 18 at 3:08

If anything, it would have to be b. You can't return a void, but you can return a null. Void on the function declaration should be there to notify the compiler of a lack of a returned value.

Void Type

In C and C++

A function with void result type ends either by reaching the end of the function or by executing a return statement with no returned value. The void type may also appear as the sole argument of a function prototype to indicate that the function takes no arguments. Note that despite the name, in all of these situations, the void type serves as a unit type, not as a zero or bottom type, even though unlike a real unit type which is a singleton, the void type is said to comprise an empty set of values, and the language does not provide any way to declare an object or represent a value with type void.

share|improve this answer
1  
-1: While void functions don't return any value, they still return the type void. Thus I say B is true. –  Thomas Eding Apr 22 at 22:40

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.