-4
var sum1 = 0 


func calculatorMath(arg1: Int, arg2: Int) -> Int {
        sum1 = arg1 + arg2
        return sum1
    }

calculatorMath(20,50)

//the problem is "Missing argument labels 'arg1:arg2:' in call". What do I need to do?

Nick
  • 119
  • 1
  • 1
  • 5

2 Answers2

0

I agree with Martin R. If you are using Xcode, you should have gotten an auto fix error. The correct way to call the function would be:

calculatorMath(arg1: 20,arg2: 50)

When you have an error with argument labels, make sure to check that when you call the function, you are including them.

Good Luck!

Arnav

0

It has already been suggested that you can fix this by changing the call to:

calculatorMath(arg1: 20, arg2: 50)

you can also fix it by changing the declaration to:

func calculatorMath(_ arg1: Int, _ arg2: Int) -> Int {

Explanation: In Swift each parameter can have two names; an optional external name a caller must specify, and a local name that the body of the function uses (the callee). The optional external name is listed first. If it is omitted the local name must be used by the caller, if the external name is _ (an underscore) then the caller must not use any name.

For example you could declare your function as:

func calculatorMath(_ arg1: Int, arg2: Int) -> Int {

and then a call would require no label on the first arg and one on the second are:

calculatorMath(20, arg2: 50)

Note: The Swift book tends to vary in what it calls the two names/labels:

Function Argument Labels and Parameter Names

Each function parameter has both an argument label and a parameter name. The argument label is used when calling the function; each argument is written in the function call with its argument label before it. The parameter name is used in the implementation of the function. By default, parameters use their parameter name as their argument label.

vs.

parameter → external-parameter-nameopt local-parameter-name type-annotation

external-parameter-name → identifier

local-parameter-name → identifier

Excerpts from: Apple Inc. “The Swift Programming Language (Swift 4.0.3).”

Community
  • 1
  • 1
CRD
  • 52,522
  • 5
  • 70
  • 86