Functions:
- A chunk of code that performs an action or specific task.
- Can have in-out parameters.
- Can have no-parameters to complex parameters of different types.
- When a function say it will return a value, it must return a value by the end of function execution. But the function call can ignore return value.
- Functions can return multiple values using Tuple.
- Function parameters are constant by default., if you try to change the value of a parameter it will give a compiler error.
- If you ever want to modify a parameter value, mark them as in&out parameter type.
- InOut parameters must be variables., because constants can not be modified.
- Variadic parameters can not marked as InOut.
- Place & before variable, while calling the function.
- Write inout before parameter type while defining the function.
- InOut parameters is a way to have an impact of the function out side of its scope.
- func swapTwoInts(_ a: inout Int, _ b: inout Int)
- var someInt = 3
- var anotherInt = 107
- swapTwoInts(&someInt, &anotherInt)
- Functions can have argument label and parameter name. By default argument label can become parameter name.
- func someFunction(firstParameterName: Int, secondParameterName: Int)
- func someFunction(argumentLabel parameterName: Int). With argument label and parameter name.
- func greet(person: String, from hometown: String) -> String. Use argument label for few parameters to make it more readable.
- func someFunction(_ firstParameterName: Int, secondParameterName: Int). Omit argument label by using _.
- func someFunction(parameterWithoutDefault: Int, parameterWithDefault: Int = 12). With default parameter values. These parameters can be omitted while calling the function. So for readability place them at the end. Always define your function, with no parameters at the beginning and WITH parameters at the end.
- func arithmeticMean(_ numbers: Double...) -> Double Functions with VARIADIC parameters. Can accept zero or more values of defined type and the function implementation can read this parameter as a constant array of the type. Function can have at most one variadic parameter.
- Function Type:
- Function Type as Return Parameter:
- Function type can also be a return type of another function. Usually used to determine which function to use based on a condition. For example, whether to turn on or off a switch.
- func chooseStepFunction(backward: Bool) -> (Int) -> Int. chooseStepFunction takes a Bool as input and returns a function of type (Int) -> Int.
- Function Type as Parameter Types:
- Function type can be a parameter of another function, this will make the caller to decide how to call this function.
- func printMathResult(_ mathFunction: (Int, Int) -> Int, _ a: Int, _ b: Int)
- Here mathFunction is of function type that takes two Ints and returns an Int. When calling the printMathResult function, caller can choose to pass in any function that is of same type.
- func addTwoInts(_ a: Int, _ b: Int) -> Int {
- return a + b
- }
- func multiplyTwoInts(_ a: Int, _ b: Int) -> Int {
- return a * b
- }
- Function takes two Int parameters as input and return Int.
- (Int, Int) -> Int is the function type.
- Can be used as any other types in Swift. Define a variable with function type and assign a function.
- var mathFunc: (Int, Int) -> Int = addTwoInts. This can be read as Defining a variable mathFunc of function type that takes two Int inputs and returns Int as output and set this to refer to addTwoInts function. Swift type checker will accept this.
- mathFunc(3, 2) ==> 5
- Nested Functions: Functions are usually global scope. But a function can encapsulate another function to keep the scope private and not visible to outside. Function can return one of its nested functions for global space to access and use it.
The best online casino sites for 2021
ReplyDeleteBest online 메리트 카지노 주소 casino for real money: bet365 1xbet Casino · InterTops 온카지노 – Best Online Casino for Slots with Free Spins · Jackpot City – Best Online Casino for
New on line casino platforms are popping out every month, and roulette followers are spoilt for selection. Now find a way 온라인 카지노 to|you presumably can} play European, American, and all different types of roulette regardless of your geographical location. Also, there are appreciable advantages to half in} the game online, rather at a brick-and-mortar on line casino.
ReplyDelete