Link to home
Create AccountLog in
Avatar of AshDash
AshDash

asked on

What is the difference between Function Signature and Function Declaration ?

Is there any difference between Function Signature and Function Declaration ? If so, what ?
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

The former simply specifies

a. the return type
b. the name
b. the arguments

The latter is a declaration of the function in the context of actual source code. The former can appear anywhere - say your local newspaper
Avatar of colr__
colr__

Try and get into the habit of calling functions 'methods' in java.

Method declarations and signature are similar things. An interface is used to declare a method signature, that all implementors of that interface are required and are therefore guarenteed to provide.

A method signature can be looked at as the 'external' look of a method, i.e. the method name and its parameters. This can therefore apply to an interface or a standalone method.
Just different names for the same thing, they define what you need to know to call the function
http://publib.boulder.ibm.com/infocenter/macxhelp/v6v81/index.jsp?topic=%2Fcom.ibm.vacpp6m.doc%2Flanguage%2Fref%2Fclrc07fundec.htm
ASKER CERTIFIED SOLUTION
Avatar of evilrix
evilrix
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
i think they are the same things.
@CEHJ: No way i am trying to correct you :), but didn't you told the difference between function 'definition' and signature?
>> i think they are the same things.

I can't speak for Java but they are certainly not the same thing in C++ although the difference is largely a semantic one.
>>... but didn't you told the difference between function 'definition' and signature?

No. A declaration really has to compile
SOLUTION
Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
Functionally (no pun intended) the two are quite different. The former represents the least amount of information required to define the function (method in java) fully and is for use by humans. The latter defines the function in the context of source code such that it can be compiled
SOLUTION
Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
Oho ... lots of discussion while I was typing lol.
>>declaration and definition can sometimes be used interchangeably.

That's not relevant here: the word in question is 'signature'

>>I can't speak for Java but they are certainly not the same thing in C++ although the difference is largely a semantic one.

The issue is not really language-specific. It's certainly possible to distinguish them semantically, but it's probably better to think functionally
> For C++, a "function declaration" is (as the name suggests) a declaration of a function. From the standard :
> A "function signature" on the other hand has a slightly different (although related) definition :

Nice. I've mentioned the Java 'definitions' above assuming declaration and definition are used interchangeably.
Side note :

>> assuming declaration and definition are used interchangeably.

For C++, this cannot be said. A function declaration CAN be a function definition (if it includes the function body), but it's not necessarily so.
yes thats correct
A function declaration contains the body part of the function  , while the function signature gives you the information regarding the input arguments ( their number , type and order) and the return type , if any.

boolean isGreater(int number1 , int number2)   --> function signature

function declaration is shown below...

boolean isGreater(int number1 , int number2)  {
    if(number1 > number2) {
          return true;
    } else  {
         return false;
    }
}
Avatar of AshDash

ASKER

Thank you all for the inputs and suggestions.... I think I have got the answer to my question... Just one last doubt.... Can a function signatire (in C++, to be specific) contain the 'const' keyword, the 'struct' keyword or pointers and reference (* and &) ....

To be specific is the below a valid function signature:
        void function_0(const my_class_0& argument_0,const my_class_1& argument_1,int argument_2)
>> To be specific is the below a valid function signature:
>>         void function_0(const my_class_0& argument_0,const my_class_1& argument_1,int argument_2)

ignoring the missing ; at the end, that's a function declaration, not a function signature.

And yes, it's a valid function declaration.
The accepted comment is wrong, not sure why you accepted it

a signature only has 2 components, the return type is *not* a part of the signature.
and it doesn't even explain what makes up a declaration

:)
This question was in 2 parts, Java and C++. I will make a recommendation for the C++ side but I'll leave it to my learned colleagues on the Java side to make their own recommendation for the Java part of the question.

http:#a35239318 (evilrix) provided the answer in layman's terms from a C++ point of view.
http:#a35239371 (Infinity08) provided valuable clarification from the point of view of the C++03 standard

Java chaps, over to you :)