strange call function without parameter


Analyzing a source code I've bumped into a strange function call. This function is defined with the following prototipe:

static void nl_kaodv_callback(int sock);

and the only call of this function nl_kaodv_callback is here:

if (attach_callback_func(aodvnl.sock, nl_kaodv_callback) < 0) {

alog(LOG_ERR, 0, __FUNCTION__, "Could not attach callback.");
}

as you can see the function nl_kaodv_callback is called WITHOUT any argument.

What does it mean???
closed account (zb0S216C)
It can mean two things: either the parameter has been assigned a default value, or, there's an overloaded version of it which takes not arguments. Here's an example of the two:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// Default argument:
int Function1( int Parameter = 0 )
{
    // With default arguments, you don't have to pass a value to it. Instead, it will
    // used the default value.
}

// No parameters:
int Function2( )
{
    // This function requires no arguments.
}

int main( )
{
    // Calling the default argument function:
    Function1( );

    // Over-ride the default argument value. Function1( ) will now use
    // the value of 10 instead of zero.
    Function1( 10 );

    // Calling the no-parameters function:
    Function2( );
}


Wazzak
Last edited on
@Framework:
In the OP's example the function is used as:
attach_callback_func(aodvnl.sock, nl_kaodv_callback) < 0

where the bold part was the aforementioned function.

Correct me if I am wrong, but passing a function without the parenthesis returns it address?
That is what I think is being used in the OP's example.
Look at the prototype of attach_callback_func(), probably it receives a function as an argument.
So you are not calling at nl_kaodv_callback() there, but passing its address to the other function.
I guess that is defining a function to be called when an event is triggered.

Look for function pointers and functors objects.
Well, with a name like attach_callback_func(), I think it's more than probable that it takes a function pointer!

In fact, to all intensive purposes, functions in C/C++ are pointers, in that they represent a memory address; a function is its start address.

In the case of a normal function, it's a fixed pointer. Whereas a function pointer variable can be changed when wanted (const allowing).

"to call a function" can be re-expressed as "to apply the function call operator -- aka operator() -- to a function pointer/address".

Andy

P.S. If you want to, you can write then out using cout

cout << "func addr = " << nl_kaodv_callback << endl;

Last edited on
Topic archived. No new replies allowed.