Thread: Function declaration question

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    66

    Function declaration question

    Hi. I was browsing through the php source code, and I found the following function prototype.

    PHPAPI char *php_strtoupper(char *s, size_t len);

    I saw this kind of declaration in many source codes, but what I don`t understand is, what is the keyword before the return type.

    Until now if I declared a function, it looked like this:
    int something() or char *something().

    Can somebody tell me, what the keyword before the return type actually does? Is it some kind of label? Or even if it would be some macro defined elsewhere, how it changes the return value of the function? And if you can give an example how to create a function like this, I would really appreciate.

    P.s.: Evidently I`m not interested in what the PHPAPI keyword means in the php source code, just in general, the first expression in these kind of declarations.

    Thank you.
    Last edited by raczzoli; 02-28-2012 at 02:18 PM.

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    It's probably defining a "calling convention".

    For example, WINAPI in windows is defined as __stdcall, which is the standard calling convention. Calling conventions have to do with whether the callee or caller cleans up the stack and the order in which the parameters are pushed or whether they're passed in registers.

    Search the PHP header files for the #define statement that defines it to see what it is.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    It could literally be anything.

    `PHPAPI' is just a macro.

    I'm guessing, but don't know, that it expands to `__attribute__((dllimport))' or something depending on the environment.

    Ultimately though, you have no business caring what it does because it isn't a special case of declaration; it is a special class of declaration.

    I know that probably didn't make sense. What I'm saying is that while `PHPAPI' or `WIN32API' all work by augmenting the function interface or how it is treated by the compiler and linker how to denote this augmentation is different depending on the environment (compiler and linker for sure).

    In other words, `PHPAPI' may expand to `__attribute__((dllimport))' for one compiler, `__declspec(dllimport)' on a different compiler, and nothing at all on a different compiler.

    Soma

  4. #4
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    phantomotap is right as usual. In php.h it's defined like this:
    Code:
    #ifdef PHP_WIN32
    #	include "tsrm_win32.h"
    #	include "win95nt.h"
    #	ifdef PHP_EXPORTS
    #		define PHPAPI __declspec(dllexport)
    #	else
    #		define PHPAPI __declspec(dllimport)
    #	endif
    #	define PHP_DIR_SEPARATOR '\\'
    #	define PHP_EOL "\r\n"
    #else
    #	if defined(__GNUC__) && __GNUC__ >= 4
    #		define PHPAPI __attribute__ ((visibility("default")))
    #	else
    #		define PHPAPI
    #	endif

  5. #5
    Registered User
    Join Date
    May 2011
    Posts
    66
    Thank you very much for the replies.
    Now I understand the role of these kinds of attributes.

    Have a good day.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. function declaration in Main function
    By filipn in forum C Programming
    Replies: 2
    Last Post: 11-11-2009, 01:31 PM
  2. function declaration.
    By jas_atwal in forum C Programming
    Replies: 4
    Last Post: 01-21-2008, 04:34 AM
  3. Replies: 2
    Last Post: 11-27-2007, 01:01 AM
  4. function declaration
    By pash in forum C Programming
    Replies: 0
    Last Post: 07-05-2002, 11:31 AM
  5. Help with function declaration
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 06-20-2002, 09:46 AM