user83039
user83039

Reputation: 3167

What are 3 items in a Swift method parameter for?

Example:

mutating func moveByX(deltaX: Double, y deltaY: Double)

The first parameter takes a Double and saves it in that method scope as deltaX. However, what are y and deltaY?

I read this and can't find anything about it: https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Methods.html

Upvotes: 2

Views: 178

Answers (4)

Robert Wagstaff
Robert Wagstaff

Reputation: 2674

Methods in swift have both an external parameter name and a local parameter name. External is defined first then external, if only one is defined swift compiler puts in the defaults.

Swift gives the first parameter name in a method a local parameter name by default, and gives the second and subsequent parameter names both local and external parameter names by default.

In the example, "y" is the external parameter used when calling the method, "deltaY" is the variable name used in the internal calculations of that function.

You can also use _ to signify that you don't want a parameter to have an external name. # is used for shorthand when both your external and internal name are the same.

Examples

1)

func exampleFunction(externalVarName1 localVarName1: Int, externalVarName2  localVarName2: Int) {}

is called like this:

 exampleFunction(externalVarName1: 0, externalVarName2: 0)

2)

func exampleFunction2(autocompleteHintName: Int, autocompleteHintName2: Int) {}

is called like this

exampleFunction2(0, 0)

3)

func exampleFunction3(#nameForBoth: Int, #nameForBoth2: Int) {}

is called like this

exampleFunction3(nameForBoth: 0, nameForBoth2: 0)

4)

func exampleFunction4(nameForBoth nameForBoth: Int, nameForBoth2 nameForBoth2: Int) {}

is the same as 3) but throws a warning that the # shorthhand can be used. called like this

exampleFunction4(nameForBoth: 0, nameForBoth2: 0)

5)

func exampleFunction5(_: Int, _: Int) {}

is called like this

 exampleFunction5(0, 0)

Upvotes: 1

Brian Tracy
Brian Tracy

Reputation: 6831

This is how Swift mimics Objective C's named parameters (pseudo naming of arguments). Its pretty easy to break down.

mutating func moveByX(deltaX: Double, y deltaY: Double)
                1        2      3     4   5       6
  1. Beginning of method name
  2. First parameter name
  3. First parameter type
  4. Second part of method name
  5. Second parameter name
  6. Second parameter type

In this example, the method is actually called moveByX:y: The equivalent Objective C method definition would look like this.

- (void)moveByX:(Double)deltaX y:(Double)deltaY

When calling this method, the second part of the name is included alone with the rest of the arguments.

var dub = Double(0.5)
moveByX(dub, y: dub)

Upvotes: 2

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726579

In your example,

If you are familiar with Objective-C, this corresponds to a method with the following declaration:

-(void)moveByX:(double)deltaX y:(double)deltaY;

Upvotes: 2

ad121
ad121

Reputation: 2368

deltaX and deltaY are the parameter names when you are writing the function. However, when you call the function it will be called as movebyX(val, y: val) where val is replaced by the Double you are passing into the function. The y in the middle is essentially to help the readability of the function when it is called and is common practice in swift to make your code as readable as possible (the person calling your function can easily tell what each parameter is without looking at the function header).

Upvotes: 0

Related Questions