-1

EDIT I changed it according to some suggestions but I am now getting different errors

I'm trying to write a function that reads an array and if the number of elements is less than 10, then it continues to add generated values into it.

To do so I made this function:

func arrayValueInputter(w : Array<Int>){

    var x = 0;

    repeat{

        x += 2;

        w.append(x);

    } while w.count < 10

    print(w);
}


arrayValueInputter([1, 3, 9, 10]);

print("End Here");

However everytime I run it I get the error:

OLD ERROR

error: extraneous argument label 'w:' in call
arrayValueInputter(w: [1, 3, 9, 10]);

OLD ERROR

NEW ERROR

Untitled-2.swift:12:5: error: cannot use mutating member on immutable value: 
'w' is a 'let' constant
 w.append(x);
^
Untitled-2.swift:20:20: error: missing argument label 'w:' in call
arrayValueInputter(&arr);

NEW ERROR

I don't know what I'm missing or maybe I'm using the loop wrong. Since, I just started learning swift.

user28434'mstep
  • 6,290
  • 2
  • 20
  • 35
Nen
  • 125
  • 1
  • 9
  • just erase the "_" before the w on method declaration. If you use _ you dont need to put argument label when you call the method – Nathan Barreto Aug 03 '18 at 12:51
  • 1
    either change the signature to `func arrayValueInputter(w: inout Array) { ... }` or invoke it like `arrayValueInputter([1, 3, 9, 10]);` (but you need to make the input __mutable__ as well in both cases for `inout`). – holex Aug 03 '18 at 12:52
  • check https://www.programiz.com/swift-programming/function-parameter-return-values#argument-label – Nathan Barreto Aug 03 '18 at 12:54
  • Possible duplicate of [When are argument labels required in Swift?](https://stackoverflow.com/questions/24815832/when-are-argument-labels-required-in-swift) – Nathan Barreto Aug 03 '18 at 12:56
  • I'm rolling this back a ways since it seems like you're trying to add more questions to your single question. We prefer one question per...question, please. If you've got another problem, feel encouraged to ask another question. – Makoto Aug 03 '18 at 14:38
  • @Makoto the question is still the same question after taking on some changes the problem got more complicated but never deviated from the issue with passing an array through a function in swift – Nen Aug 03 '18 at 14:42

1 Answers1

2

Two issues:

  1. The error says you have to omit the parameter label w
  2. The other serious issue is that you can't use a literal as an inout parameter

var array = [1, 3, 9, 10]
arrayValueInputter(&array)

Regarding NEW ERROR

Again two issues:

  1. missing argument label 'w:' in call means: As you removed the underscore now you have to use the parameter label. The underscore indicates that the label is going to be ignored.

    arrayValueInputter(w: [1, 3, 9, 10]);
    
  2. cannot use mutating member on immutable value means: All passed parameters are constants (let) by default. Assign the value to a new local var iable with the same name.

    func arrayValueInputter(w : Array<Int>){
        var w = w
        var x = 0
    ...
    

And this is Swift: No trailing semicolons.

vadian
  • 274,689
  • 30
  • 353
  • 361
  • I was trying to make it mutable and after some googling couldn't find any other way, and I tried your correction but it didn't work stuck with the errors I now defined in the edit – Nen Aug 03 '18 at 13:34
  • I updated the answer. – vadian Aug 03 '18 at 14:11
  • thanks mate you're the best – Nen Aug 03 '18 at 14:33
  • @vadian I kinda wish you hadn't answered with new error, since that made rolling back the chameleon-like question a lot tougher... – Makoto Aug 03 '18 at 14:40