4

I have a simple question:

What is the type of function in C or C++

As we can have pointers to function in C or C++, that means functions should have a specific type otherwise type checking during pointers to function creation have no meaning.

Can someone explain me, I am on the correct path or not?

If I am on the right path, How can I find the type of function?

Mat
  • 202,337
  • 40
  • 393
  • 406
neel
  • 8,399
  • 7
  • 36
  • 50
  • 1
    Do you know how to declare a pointer-to-function variable? If so, you know the answer to your question already. – Mat Oct 25 '12 at 11:08
  • `typeid(function).name()` will usually return a user readable type for you. – Yakov Galka Oct 25 '12 at 11:12
  • C/C++ don't have first class functions so they don't really have an actual type. – Pubby Oct 25 '12 at 11:13
  • 1
    they are void function pointers. they are a special void type :) – Shark Oct 25 '12 at 11:23
  • 1
    @Pubby: Of course functions have types. One might say they're not "first class" types since you can't declare a variable of that type; but they certainly are actual types. – Mike Seymour Oct 25 '12 at 12:28
  • I came to the same conclusion as @Shark. If we look at function as a type, it's just a void pointer. – pronebird Mar 15 '13 at 14:11

4 Answers4

9

Syntax for function pointers

The type of a function in C/C++ includes both the return type and the types of input parameters .

Consider the following function declaration:

int function(char, float);

A pointer to that function has the following type:

int (*funptr)(char, float); 

Similarly in general :

returntype function (argtype1, argtype2, argtype3)

A corresponding pointer to such a function is

returntype (*ptr) (atgtype1, atgtype2, atgtype3);  

There are be many different types of functions. Find a useful reference on function pointers here.

Also, this classification is based on the return type and argument types. Functions can also be classified on the basis of scope of their accessibility. like global functions, static functions etc. See here for a short introduction.

phant0m
  • 16,595
  • 5
  • 50
  • 82
Grijesh Chauhan
  • 57,103
  • 20
  • 141
  • 208
7

Of course every function has it types,

for example, function

double foo(bar& f, const const baz*)

has a type of

function, that accepts reference to bar and constant pointer to baz and return double

It can be written like

double ()(bar&, const baz*)

A pointer to variable of types of that function will have type (variable that can store pointer to that function)

will have type

double (*)(bar&, const baz*)

Or, if you want to typedef a pointer to functions of that type you can write

typedef double (*func_ptr)(bar&, const baz*)

Again,

func_ptr is a type of pointer to function, that accepts reference to bar and constant pointer to baz and return double

One thing here is that function decays to pointer to function, so you can write

func_ptr f = &foo;

and

func_ptr g = foo;

And it would be the same.


Now imagine, that you have

struct A
{
    double goo(bar& f, const const baz*);
};

Now goo has a type of

function of struct A, that accepts reference to bar and constant pointer to baz and return double

A pointer to this function will have type

double (A::*)(bar&, const baz*)

Note, that it types differs from type of free function foo. They are not compatible at all.

However, if goo were static function, the fact that it belongs to struct A would be insufficient (as far as member function requires implicit this argument and static function does not).

Andy Zhang
  • 198
  • 4
  • 21
Lol4t0
  • 12,444
  • 4
  • 29
  • 65
2

It's actually function signature which should match either with declaration or with function pointer

Function signature contains everything as such arguments type , no of arguments and return type.

Directly like variables you cannot say that particular function is of int type or float or char type or so on

Always remember it's signature as i said above.

Omkant
  • 9,018
  • 8
  • 39
  • 59
  • Function's signature do not contain return type always, You can have a look here http://stackoverflow.com/questions/290038/is-the-return-type-part-of-the-function-signature – neel Oct 25 '12 at 11:15
  • Yeah, but only on the basis of return type you cannot do function overloading .. or distinguish a function . because returned value may be ignored by our requirement.. sometimes it's not needed – Omkant Oct 25 '12 at 11:17
  • 2
    A function has a type, just like everything else. The type is based on its signature, but it is part of the type system. (Unlike the signature, the type also includes the return type.) – James Kanze Oct 25 '12 at 11:18
  • @neel : Yeah but if we don't mention return type then it will be problem in creating a function pointer. – Omkant Oct 25 '12 at 11:22
  • @Omkant thats what I am saying function signature is not same as function type. – neel Oct 25 '12 at 11:24
  • neel : read my third line of my answer also the comments which James Kanze has written "the type is based on it's signature".. – Omkant Oct 25 '12 at 11:27
-2

Every pair of two types (A,B) has a specific function type A->B. If we choose A=int, B=float, then the function type would become:

float my_function(int a);
tp1
  • 1,197
  • 10
  • 17