Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

One question about function definition in C++

I'm reading some material about function pointer in C++, and come across one function definition which I do not understand.
Standard function definition have the form:

type name (param...)

But the following definition seems a little strange to me. Can anyone explain it to me ? Thanks.

float (*GetPtr1(const char opCode)) (float, float)<br>
{
    if(opCode == '+')
        return &Plus;
    else
        return &Minus; // default if invalid operator was passed
}


Note: Plus and Minus are two functions with param (float, float) and return a float.

like image 912
cheng Avatar asked Nov 15 '10 13:11

cheng


People also ask

What is the function definition in C?

We refer to a function as a group of various statements that perform a task together. Any C program that we use has one function at least, that is main(). Then the programs can define multiple additional functions in a code.

What is function definition in C with example?

A function definition provides the actual body of the function. The C standard library provides numerous built-in functions that your program can call. For example, strcat() to concatenate two strings, memcpy() to copy one memory location to another location, and many more functions.

Why functions are needed in C?

By using functions, we can avoid rewriting same logic/code again and again in a program. We can call C functions any number of times in a program and from any place in a program. We can track a large C program easily when it is divided into multiple functions. Reusability is the main achievement of C functions.

What is function definition in C Explain with syntax?

A function definition in C programming language consists of function name, function parameters, return value and function's body. First line is called as Function Header and it should be identical to function Declaration/Prototype except semicolon. Name of arguments are compulsory here unlike function declaration.


3 Answers

GetPtr1 is a function that takes an opcode char and returns a pointer to a function. The function it returns takes two floats and returns a float.

A lot of times it's easier to read if you do something like this:

typedef float (*FloatOperationFuncPtr) (float, float);

FloatOperationFuncPtr GetPtr1(const char opCode)
{
    if(opCode == '+')
        return &Plus;
    else
        return &Minus; // default if invalid operator was passed
}
like image 146
Lou Franco Avatar answered Oct 15 '22 00:10

Lou Franco


The rule for reading hairy declarations is to start with the leftmost identifier and work your way out, remembering that () and [] bind before * (i.e., *a[] is an array of pointers, (*a)[] is a pointer to an array, *f() is a function returning a pointer, and (*f)() is a pointer to a function):

        GetPtr1                                       -- GetPtr1
        GetPtr1(                 )                    -- is a function 
        GetPtr1(           opCode)                    -- taking a single parameter named opCode
        GetPtr1(const char opCode)                    -- of type const char
       *GetPtr1(const char opCode)                    -- and returning a pointer
      (*GetPtr1(const char opCode)) (            )    -- to a function
      (*GetPtr1(const char opCode)) (float, float)    -- taking two parameters of type float
float (*GetPtr1(const char opCode)) (float, float)    -- and returning float

So, if opCode is equal to '+', GetPtr1 will return a pointer to the function Plus, and if it's '-', it will return a pointer to the function Minus.

C and C++ declaration syntax is expression-centric (much as Bjarne would like to pretend otherwise); the form of the declaration should match the form of the expression as it would be used in the code.

If we have a function f that returns a pointer to int and we want to access the value being pointed to, we execute the function and dereference the result:

x = *f();

The type of the expression *f() is int, so the declaration/definition for the function is

int *f() { ... }

Now suppose we have a function f1 that returns a pointer to the function f defined above, and we want to access that integer value by calling f1. We need to call f1, derefence the result (which is the function f), and execute it, and then dereference that result (since f returns a pointer):

x = *(*f1())(); // *f1() == f, so (*f1())() == f() and *(*f1())() == *f()

The type of the expression *(*f1())() is int, so the decaration/definition for f1 needs to be

int *(*f1())() { return f; }
like image 44
John Bode Avatar answered Oct 15 '22 01:10

John Bode


Always nice to know about http://cdecl.org for such situations. Be aware that it only works if you remove the parameter names. This is what you get for float(*GetPtr1(const char ))(float, float):

declare GetPtr1 as function (const char) returning pointer to function (float, float) returning float

like image 29
Björn Pollex Avatar answered Oct 15 '22 02:10

Björn Pollex