Search Your Question

What is inout parameter in swift?

 Function parameters are constants by default. Trying to change the value of a function parameter from within the body of that function results in a compile-time error. This means that you can’t change the value of a parameter by mistake. If you want a function to modify a parameter’s value, and you want those changes to persist after the function call has ended, define that parameter as an in-out parameter instead.

func add(x: Int, y: Int) -> Int
{
x=7              error: Can not assign to value x, x is let constant
return(x+y)
}

func add(x: inout Int, y: Int) -> Int
{
x=7           
return(x+y)
}

Bonus Tip : You can run swift code online here
  

No comments:

Post a Comment

Thanks