Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

grandM

macrumors 68000
Original poster
Oct 14, 2013
1,508
298
I am struggling with the difference between definition and declaration in Swift. It would be an enormous help if you could first explain the difference in Objective-C. Thank you!
 

casperes1996

macrumors 604
Jan 26, 2014
7,485
5,649
Horsens, Denmark
I am struggling with the difference between definition and declaration in Swift. It would be an enormous help if you could first explain the difference in Objective-C. Thank you!

In what context have you read the terms? Sometimes terms get used interchangeably even when they strictly speaking have unique meanings, like how one might say function or method interchangeably even though they do have somewhat different intended meanings.
I've never seen "definition" as having a strict meaning but more be used for either initialisation and/or declaration. Or I guess when talking about functions the definition is also the term for the function signature.

I never got familiar with Objective-C, but

Code:
var namedVariable: Int
Declares a variable. Arguably also defines it, but it only defines its existence

Code:
var namedVariable: Int = 42
Declares the variable and defines it to hold the value 42.

So yeah the way I see it at least definition is just declaration (+ initialisation)
I've never seen the term more formally defined, but I'm curious about the context
 

chown33

Moderator
Staff member
Aug 9, 2009
10,766
8,466
A sea of green
For C functions, a declaration only declares the name and types for the function. There is no function body.

Conversely, a definition provides all the same name and type info as the declaration, along with the function body. The definition's name, arg types, and return type must match any prior declaration with the same function name.

A program can contain any number of declarations, as long as they're consistent. There can be only one definition, otherwise the linker will fail with multiple definitions for the same symbol.


For C variables, a declaration again declares name and type, but omits any initializer. There may or may not be an 'extern' storage qualifier.

Again, there can be multiple declarations of a variable in a program as long as they're consistent. And again, there can be only one definition with an initializer.


For functions, the thing that distinguishes definitions from declarations is the presence of a function body. For variables, the distinguishing characteristic is an initializer.
 

grandM

macrumors 68000
Original poster
Oct 14, 2013
1,508
298
I also noticed the concepts being interchangingly used. It seems in Swift they mostly coincide for structures, classes. For protocols they may differ. Your example above is correct. The initialization with a value seems to define it. Not sure if an assignment later on is a definition. Question remains if one can speak about namedVariable being defined on line 3? Put differently is the declaration part of the definition?
var namedVariable: Int
var namedVariable2: Int
namedVariable = 2

Structures, classes are a different story. Objective-C had a .h file and a .m file. .h contained the header and would coincide with the interface in Swift: the properties (and type) plus the methods (and ingoing and outgoing parameters). It declared the structure or class. .m implemented the properties and also the getters and setters declared in the .h file. The declared properties and methods were defined. Not sure if the declaration + implementation was the definition or only the .m implementation file. I guess the latter. In Swift the structure or class definition no longer has 2 separate files so the declaration file disappeared. If you create the interface it 'd have been the .h file though. In Swift declaration and definition became one as thus as far as structures and classes is concerned.

In Swift the distinction becomes clear when we're talking protocols. If the protocol does not implement itself the implementation is left to the classes or structures conforming to it. The conforming class knows a function is present/was declared but must still implement/define it. Protocol members were also only declared in the protocol.

I have the impression in all former examples the implementation or initialization is the definition. This also could mean that the declaration need not be part of the definition. In swift however it often is. Somehow designing Swift they decided to place declaration central. Something is declared. In most cases the declaration will hold its implementation or initialization. For variables/constants that be the initialization. Structures, classes the declaration holds the implementation consequently we often read 'the class definition.' Protocol members however are declared but not defined.

The documentation blends the terminology and had better clearly outlined what both meant. '
A declaration introduces a new name or construct into your program. For example, you use declarations to introduce functions and methods, variables and constants, and to define new, named enumeration, structure, class, and protocol types. You can also use a declaration to extend the behavior of an existing named type and to import symbols into your program that are declared elsewhere.

In Swift, most declarations are also definitions in the sense that they are implemented or initialized at the same time they are declared. That said, because protocols don’t implement their members, most protocol members are declarations only. For convenience and because the distinction isn’t that important in Swift, the term declaration covers both declarations and definitions.'
 

grandM

macrumors 68000
Original poster
Oct 14, 2013
1,508
298
For C functions, a declaration only declares the name and types for the function. There is no function body.

Conversely, a definition provides all the same name and type info as the declaration, along with the function body. The definition's name, arg types, and return type must match any prior declaration with the same function name.

A program can contain any number of declarations, as long as they're consistent. There can be only one definition, otherwise the linker will fail with multiple definitions for the same symbol.


For C variables, a declaration again declares name and type, but omits any initializer. There may or may not be an 'extern' storage qualifier.

Again, there can be multiple declarations of a variable in a program as long as they're consistent. And again, there can be only one definition with an initializer.


For functions, the thing that distinguishes definitions from declarations is the presence of a function body. For variables, the distinguishing characteristic is an initializer.
We're getting there.
  1. So the .m file is the definition in Objective-C?
  2. In Swift is the initialization of a previously declared variable the definition of it or must it hold the declaration too (I guess not?)
  3. It seems you can redeclare a variable in C? Am I correct this cannot be done in Swift?
 

casperes1996

macrumors 604
Jan 26, 2014
7,485
5,649
Horsens, Denmark
At least by default both my C compiler and my Swift compiler will complain about redeclaration in the same scope (and compilation unit). You can shadow variables with both languages though, i.e. redeclare in a more local scope.

I think a problem with trying to apply the term definition to setting a value after declaration is that the program flow might mean the line is not necessarily the initialisation but could just be changing the value of the variable and I don't think most people would call a mutation a definition, but I dunno. To me the key thing here really is this:

In Swift, most declarations are also definitions in the sense that they are implemented or initialized at the same time they are declared. That said, because protocols don’t implement their members, most protocol members are declarations only. For convenience and because the distinction isn’t that important in Swift, the term declaration covers both declarations and definitions.'

But to answer nr. 1; I would say in C/C++ your .h/.hh file is declaration and .c/.cpp is definitions, yes.
 
  • Like
Reactions: grandM

Senor Cuete

macrumors 6502
Nov 9, 2011
424
30
In C a function prototype is usually required. This is a declaration that indicates the types of the function's parameters and return value. For example:
Code:
double foo(double bar);
takes a type double and returns a double. The parameter name bar is not required but is usually used because it makes the code more readable. Prototypes can be in the same file as the function's definition or in a .h header file. If you want to use the function in another file you can #include or #import the name of the header file in the .c file.

A function definition includes the actual code for the function. For example:
Code:
double foo(double bar)
{
return bar * M_PI;
}

In Objective C an object's functions are called methods and are in the same file .m file as the code for the object.
 
  • Like
Reactions: grandM

grandM

macrumors 68000
Original poster
Oct 14, 2013
1,508
298
In C a function prototype is usually required. This is a declaration that indicates the types of the function's parameters and return value. For example:
Code:
double foo(double bar);
takes a type double and returns a double. The parameter name bar is not required but is usually used because it makes the code more readable. Prototypes can be in the same file as the function's definition or in a .h header file. If you want to use the function in another file you can #include or #import the name of the header file in the .c file.

A function definition includes the actual code for the function. For example:
Code:
double foo(double bar)
{
return bar * M_PI;
}

In Objective C an object's functions are called methods and are in the same file .m file as the code for the object.
The .m holds the definitions so. Wonder if implementations and definitions are synonyms?
 

casperes1996

macrumors 604
Jan 26, 2014
7,485
5,649
Horsens, Denmark
The .m holds the definitions so. Wonder if implementations and definitions are synonyms?
I would say they sort of are, but have connotative differences and one just feels more appropriate in some contexts than the other. Like if I right click a function call in an editor I expect to see an option "Show Definition" that jumps to where it's defined. But if I'm talking to someone I can't imagine I'd ever say "And how have you defined foo?" I'd be way more likely to say "And how have you implemented foo?"
 

grandM

macrumors 68000
Original poster
Oct 14, 2013
1,508
298
I would say they sort of are, but have connotative differences and one just feels more appropriate in some contexts than the other. Like if I right click a function call in an editor I expect to see an option "Show Definition" that jumps to where it's defined. But if I'm talking to someone I can't imagine I'd ever say "And how have you defined foo?" I'd be way more likely to say "And how have you implemented foo?"
If they are the same it pretty much only adds to confusion.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.