Reputation: 11484
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
Upvotes: 0
Views: 83
Reputation: 7373
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)
Upvotes: 0
Reputation: 8391
// 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
}
Upvotes: 1
Reputation: 3863
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.
Upvotes: 2