How to edit function input in Swift?

swift pass function as parameter
inout swift
swift assign function to variable
for loop swift
swift anonymous function
swift function optional parameter
swift default parameter
swift function type

I am building an idle clicker app in Swift and I am adding in auto clicks through a function. I want to be able to enter the price through the input for the function, but because the input is a constant, I can't add to the cost price by 9% like I want every time the user clicks the upgrade.

For example if I type 50.0 into the initialPrice how would I make it so that it increases by 10% every time the user clicks it.

func upgrade(intialPrice : Float) {
    var upgradePrice = intialPrice
    AutoClicks += 1
    totalClicks = totalClicks - Int(upgradePrice)
    upgradePrice = upgradePrice + (upgradePrice * 0.1)
    burritoLvl1Label.text = ("$" + "\(Int(upgradePrice))")
}

I am very new to coding so if you see any way I could make this shorter that would be very helpful.


The other answers have suggested using an inout parameter, and while this will work, it is not a pattern I would recommend. Generally it is better to avoid "side effects" in functions and use inout only when there is a very good reason.

The more general approach would be to have a function that returns the new value.

You might also want to consider using a Decimal rather than a Float when dealing with currency.

Similarly, updating the label probably shouldn't be in this function

func upgrade(initialPrice : Decimal) -> Decimal {
    autoClicks += 1
    totalClicks -= Int(truncating: initialPrice as NSDecimalNumber)
    return initialPrice * 1.1
}

You can then call this function like so:

price = upgrade(initialPrice: price)
burritoLvl1Label.text = ("$" + "\(Int(truncating:price as NSDecimal))")

Functions, Every function in Swift has a type, consisting of the function's parameter types and return type. To use a function, you “call” that function with its name and pass it input If you want a function to modify a parameter's value, and you want those  Function Definition. In Swift 4, a function is defined by the "func" keyword. When a function is newly defined, it may take one or several values as input 'parameters' to the function and it will process the functions in the main body and pass back the values to the functions as output 'return types'.


You need to pass the value in as an inout argument to be able to mutate it. You can also make your implementation more concise by using the compound assignment operators, +=, -= and *=. You should also make sure that you conform to the Swift naming convention, which is loweCamelCase for function and variable names (autoClicks) and UpperCamelCase for types.

func upgrade(initialPrice: inout Float) {
    autoClicks += 1
    totalClicks -= Int(upgradePrice)
    upgradePrice *= 1.1
    burritoLvl1Label.text = ("$" + "\(Int(upgradePrice))")
}

However, you should be aware that in most cases you should return the mutated value from a function instead of mutating it by passing it in as an inout parameter. It is especially bad practice to mutate some instance variables of your class without passing them into the function explicitly, while modifying some others using an inout argument.

Swift Function Parameters and Return Values (With Examples), These type of functions take input parameter but do not return value. If you want a function to modify a parameter's value, you need to define the parameter as  print("Hi " + firstName + " " + lastName + "!") } sayHi("Bob", lastName:"Smith") In the parentheses, we have a comma-separated list of input parameters to the function. Each one needs a parameter name so we can refer to them inside of the function, and each one needs a type annotation as well.


You should make your input parameter as inout.

More info about swift functions.

func upgrade(intialPrice: inout Float) {
    var upgradePrice = intialPrice
    AutoClicks += 1
    totalClicks = totalClicks - Int(upgradePrice)
    upgradePrice = upgradePrice + (upgradePrice * 0.1)
    burritoLvl1Label.text = ("$" + "\(Int(upgradePrice))")
}

Hope it helps!

Swift Basic Input and Output (With Examples), All parameters passed into a Swift function are constants, so you can't change them. If you want, you can pass in one or more parameters as inout , which means  Library functions are built-in functions that are already defined in Swift framework. These functions are present to solve common problems in Swift so that you don't have to solve them yourselves. They are simple operations like printing, finding minimum and maximum, etc. You can use library function directly by invoking (calling) it. If you


inout parameters, value; //can't change value of input param3. let complementValue = !value4. print(​“complementValue := \(complementValue)”)5. return  To use the math-functions you have to import Cocoa. You can see the other defined mathematical functions in the following way. Make a Cmd-Click on the function name sqrt and you enter the file with all other global math functions and constanst. A small snippet of the file


Swift Function Parameters : letParam | varParam, Swift - Functions - A function is a set of statements organized together to perform a To use a function, you "call" that function with its name and pass input values and it is returned back 'out' of the function to modify the original argument. So inside function buttonClick we will get input from the Text Field. To do this modify the code of your ViewController.swift file as follows. // // ViewController.swift // FirstIphoneApp // // Created by Belal Khan on 26/07/16.


Swift - Functions, My opinions are likely to change as I gain more experience using the new To enable functions that don't accept any input to still be called  Swift string input has become a lot easier than since I posted an article about it. In my previous article Swift string input required you to create your own function to read a input line.