0

A noob question that probbaly applies to C as well as C++. Let's say I have

void myfunc() {
    blah;
}

So, I call this function with:

myfunc();

However, no compiler error is produced when I "call" it with:

myfunc;

Program runs, but myfunc doesn't get called. So, what is C++ interpreting this as?

Now, I'm doing this in the Arduino IDE, all one big lump of code, so I don't get segfaults, etc. So maybe this would throw a runtime error on a dynamically linked host.

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
wsanders
  • 300
  • 5
  • 9

3 Answers3

3

myfunc without the parens is the address of the function in memory.

For example, if you have to pass a function to some other function, you would do it with that.

A good example of this is in bsearch in the c standard library, where you need to pass a user defined comparator function in order to do a generic search.

yamafontes
  • 5,552
  • 1
  • 18
  • 18
  • 5
    `myfunc` without parentheses is not a pointer to the function. It is the function. See [Why do all these crazy function pointer definitions all work?](http://stackoverflow.com/questions/6893285/why-do-all-these-crazy-function-pointer-definitions-all-work-what-is-really-goi/6893288#6893288) for details. – James McNellis Oct 23 '13 at 02:54
1

The compiler just evaluates the expression. Since you're evaluating the name of a function, it's basically a no-op.

It's just like this:

int main() {
    42; // evaluates 42 but does nothing with it
}

Your compiler should warn you that the result of the expression is unused, anyway.

mfontanini
  • 21,410
  • 4
  • 65
  • 73
1

In C myfunc or any other function name represents the function itself, which will be implicitly converted to a function pointer

Function to pointer

An lvalue of function type T can be implicitly converted to a prvalue pointer to that function. This does not apply to non-static member functions because lvalues that refer to non-static member functions do not exist.

https://en.cppreference.com/w/cpp/language/implicit_conversion#Function_to_pointer

and () is an operator that when applies to a function pointer or a function object will invoke that function

Built-in function call operator

The function call expressions have the form

E ( A1, A2, A3,... )

where

  • E is an expression that names a function
  • A1, A2, A3,... is a possibly empty list of arbitrary expressions, except the comma operator is not allowed at the top level to avoid ambiguity.

The expression that names the function can be

  1. lvalue expression that refers to a function
  2. pointer to function
  3. explicit class member access expression that selects a member function
  4. implicit class member access expression, e.g. member function name used within another member function.

So without the function-call operator myfunc; is just a no-op expression that contains a function pointer. If you've turned on compiler warnings (which you should really do) then they'd shout at you about the issue. GCC says that

statement is a reference, not call, to function 'func' [-Waddress]
warning: statement has no effect [-Wunused-value]

while Clang outputs warning: expression result unused [-Wunused-value]

Community
  • 1
  • 1
phuclv
  • 37,963
  • 15
  • 156
  • 475