-3

I am very new to Swift, actually I started learning it today. Can someone tell me whats wrong with calling the function in this way?

func myAddition(i:NSInteger,j:NSInteger) ->NSInteger {
    i
    j
    return i+j
}

myAddition(5, 6)

Edit I fixed the error by inserting j:

myAddition(5, j: 6)

is this normal? I am following a tutorial and there its working without j

User1932
  • 1
  • 3

3 Answers3

0

You have to call method in this way:

 myAddition(5, j: 6)

Try this it may help you.

Bhumika
  • 876
  • 7
  • 20
  • Thank you it works but can you explain me why? In the tutorial its working without the j: – User1932 Jun 20 '16 at 10:42
  • @User1932 Visit this link it will help you to understand. http://stackoverflow.com/questions/24045890/why-does-a-function-call-require-the-parameter-name-in-swift – Bhumika Jun 20 '16 at 10:52
0
func myAddition(i:Int,j:Int) ->Int {
    return i+j
}

print(myAddition(5, j: 6))

This will work for you.

onemillion
  • 682
  • 5
  • 19
0

If you want to be able to call the function without specifying the parameter names, then you have to tell Swift that there are no external parameter names for the function by using the underbar (_) as the external parameter name:

func myAddition(_ i:NSInteger, _ j:NSInteger) -> NSInteger {
    return i+j
}

myAddition(5, 6)

Do you really need an NSInteger as opposed to a normal Swift Int:

func myAddition(_ i:Int , _ j:Int) -> Int {
    return i+j
}

myAddition(5, 6)
Steve Ives
  • 7,894
  • 3
  • 24
  • 55
  • I think the need to specify the first parameter name on the function call has changed between different versions of Swift , which is why you might not be seeing the same results as the tutorial. – Steve Ives Jun 20 '16 at 10:56