swift - How can we change Function Parameter? -


in swift can change values of local parameters of function in function?

like:

 func examplefunction(value : string, index : int) -> bool         {          value = "changed value" // showing error          index = 3  // showing error         return true         } 

calling function in viewdidload():

 var flag  = examplefunction("passed value" , 2) 

is possible change value , index in examplefunction scope? if not there other way this? because using change value of temp.

var temp = value temp = "temp change" 

it seems example need change passed arguments inside function side effect, , need updated values available after function call. if that's case, need use inout modifier.

otherwise, if need modify parameters inside of function call, can explicitly define them variables on function definition:

using in-out parameters

first, change function declaration to:

func examplefunction(inout value: string, index: inout int) -> bool 

now, in-out parameter has value passed in function, modified function, , passed out of function, replacing original value. work, can't pass literal function, since there store modified value afterwards. in fact, has variable. cannot pass constant or literal value argument, because constants can't modified. hence, change function call to:

var passed = "passedvalue" var index = 2  var b = examplefunction(&passed, &index) 

after call, both passed , index contain new values, modified function.

also, note & before each argument when calling function. must there, indicate argument can modified function.

using variable parameters – removed in swift 3

in case, need change function declaration use variable parameters, below:

func examplefunction(var value: string, var index: int) -> bool 

changes made arguments inside scope of function, aren't visible outside function or stored anywhere after function call.


Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -