Missing argument for paramater #1 in call

I have this function and variable:

var trianglePlacement: CGFloat

func setPlacmentOfTriangle(trianglePlacementVal: CGFloat) {
    trianglePlacement = trianglePlacementVal
}

I use the function later to do a calculation for this variable:

var trianglePosition: CGFloat = setPlacmentOfTriangle() * width

I am getting a Missing argument for parameter #1 in call error, okay makes sense, I am not passing in an argument.

Is there are way to get rid of the error without getting rid of the argument and not passing in an argument? These are the only times they show up in my code.

In case you are curious, this is the variable width:

var width: CGFloat = self.menubar.frame.size.width

Answers


Yes, there is a way you can call the function without passing in an argument. You can provide a default value to the function parameter like so:

func setPlacmentOfTriangle(trianglePlacementVal: CGFloat = 1.0) {
    trianglePlacement = trianglePlacementVal
}

Then you call it like this:

setPlacmentOfTriangle()

If you call the setPlacmentOfTriangle function, it will use a value of 1.0 anytime you don't provide your own argument.

I would guess that this is a simplified example, but this function is not necessary at all. You really should just set the value of trianglePlacement directly, and not use a function like you are. Also, this line won't work:

var trianglePosition: CGFloat = setPlacmentOfTriangle() * width

The setPlacmentOfTriangle function doesn't return a value, so there is nothing to multiply with width.


// This is not very functional, avoid mutating other scopes
var trianglePlacement: CGFloat = 0

func setPlacmentOfTriangle(trianglePlacementVal: CGFloat) {
    trianglePlacement = trianglePlacementVal
}

// this is better
var trianglePlacement: CGFloat = 0

func setPlacmentOfTriangle(trianglePlacementVal trianglePlacementVal: CGFloat) -> CGFloat {
    return trianglePlacementVal
}

// this is a function with a default  param value
var trianglePlacement: CGFloat = 0

func setPlacmentOfTriangle(trianglePlacementVal trianglePlacementVal: CGFloat = 0.0) -> CGFloat {
    return trianglePlacementVal
}

This is because setPlacmentOfTriangle should take in a CGFloat as a parameter and you're not sending it one

If you do not always want to pass an argument then you can make it optional like so:

func setPlacmentOfTriangle(trianglePlacementVal: CGFloat?) {
    if let tri = trianglePlacementVal as? CGFloat {
        trianglePlacement = tri 
    }
}

Then you can pass nil as your parameter:

setPlacmentOfTriangle(nil)

Need Your Help

Method cannot be called from Class because of identifier

java

I am writing a java programming and trying to test it. Within the Class Card I have the format() statement telling it what to return. When I try to use this format on the newly created object, it t...

Implmenting Task Runner for store output in ExtJS

extjs extjs4 extjs5 sencha-architect

I'm fairly new to ExtJS and I'm kinda stuck in one of the problems so I need your help.