function
In Swift, each function has a type, including the function's parameter value type and the return value type. You can treat a function type as if it were any other normal variable type, which makes it easier to take a function as an argument to another function, or to return a function from another function. The definition of a function can be written in other function definitions, which allows for functional encapsulation within the scope of the nested function. single parameter
Single parameter//
////-Parameter personname: Parameter 1/
//Returns: return type
func SayHello (personname:string), St Ring
{Let
greeting = "Hellow," + PersonName + "!"
return greeting
}
Print (SayHello (personname: "MKJ"))
Multi-parameter
Multi-parameter/////
///-Parameters:/ /-NUM1: Parameter 1/ /-num2: Parameter 2/
//Returns: return type
func Delatatwonumber (Num1:int, Num2:int), Int
{
return num1-num2
}
print (Delatatwonumber (Num1:10, num2:1))
Let FUNCD = Delatatwonumber
funcd (11,111)
No Parameters
No parameters
/
//////Returns: return type
func sayhelloworld ()-String {
return "Hello, World"
}
Print (SayHelloWorld ())
func Printsingleslogan (slogan:string) {
print ("Woca," +slogan + "!")
}
Printsingleslogan (slogan: "CJJ")
Default Parameters
Default parameter name///////-
Parameters:/
/- string1: Parameter 1/ /-string2: Parameter 2 //- Joiner: Parameter 3/
//-Returns: Return value
func join (string1:string,string2:string,joiner:string = "+")-String
{
return string1 + joiner + string2
}
print (Join (string1: "I", string2: "You"))//"i + you \ \"
Print (Join (string1: "He", string2: "She", Joiner: "0"))//"he 0 she \ \"
variable Parameters
A mutable parameter (variadic parameter) can accept one or more values. When a function is called, you can use a mutable parameter to pass in an indeterminate number of input parameters. By adding (...) after the variable type name. ) to define a mutable parameter.
The value of the passed variable parameter is treated as an array of this type in the function body
Variable parameters////
///-Parameter numers: Parameter array/
/-Returns: Return value
func total (numers:double ...), double
{
var Totald = 0.0 for
numer in numers {
Totald + =
numer
} return totald/double (Numers.count )
}
Print ("Total is" + "\ (numers:2,10,21)")
input and output parameters (InOut)
Variable parameters, as described above, can only be changed within the function body. If you want a function that modifies the value of a parameter and wants it to persist after the function call ends, define the parameter as an input-output parameter (in-out Parameters).
When defining an input/output parameter, add the InOut keyword before the parameter definition. An input-output parameter has the value of the passed-in function, which is modified by the function, and then the outgoing function replaces the original value.
You can only use variables as input and output parameters. You cannot pass in constants or literal literals (literal value), because these quantities cannot be modified. When an incoming parameter is used as an input and output parameter, it is necessary to add the & character before the parameter, indicating that the value can be modified by the function.
Note: input and output parameters cannot have default values, and mutable parameters cannot be marked with inout. If you mark a parameter with inout, this parameter cannot be marked by Var or let.
Input/output parameters///////-
Parameters:/ /-A: External variable address A/ //-B: External variable address b
func swaptwoints (A: inout int, B:inout int) {Let
Temporarya = a
a = b
b = Temporarya
}
var someint = 3
var anothe rInt = 107
//swaptwoints (A: &someint, B: &anotherint)
swaptwoints (A: &someint, B: & Anotherint)
print ("\ (someint)" + "\ (anotherint)")
function Type Use
(parameter 1, parameter 2, Parameter 3), (return value 1, return value 2)
Can be understood as passing in three parameters of a required type, returning a Ganso with two specified types
Use the function type/////////Parameters://- A: Parameter 1/ /-B: Parameter 2/
/-Returns: Return value
Func Add (a:int,b:int)->int
{
return a+b
}
func Mutiby (a:int,b:int)->int
{
Return a*b
}
//Specifies the function type
var mathfunction:(int,int), Int = add
print ("Result:\ (Mathfunction ( 2, 3))
mathfunction = Mutiby
print ("Result:\ (mathfunction (3,5))")
//Auto-infer function type let
Mathfunc = Add
print ("Result:\ (Mathfunc (2,5))")
When we declare a variable pointing to a function, you can manually specify the type of the function, and if you do not specify it, Swift automatically makes the type inference function type as the argument
function as parameter///////-
Parameters: < #a description#>/ /-B: < #b description#& gt;
-Returns: < #return value description#>
func mathadd (a:int,b:int), int{
return a+b
}
Func Printmathresult (mathfunc: (int,int)->int,a:int , b:int) {
print ("Result-->\ (Mathfunc (b))")
}
Printmathresult (Mathfunc:mathadd, A:3, b:14)
The purpose of the Printmathresult function is to output the result of the invocation of another mathematical function of the appropriate type. It does not care how the incoming function is implemented, it only cares about the correct type of the incoming function. This allows Printmathresult to ensure that incoming function calls are correct in a type-safe (type-safe) manner. function returned as type
You can use a function type as the return type of another function. What you need to do is write a complete function type after the arrow is returned.
function as return value
/////Parameter number: < #number description#>
//-Returns: < #return value Descrip Tion#>
func goForward (number:int)->int{
return number + 1
}
func Gobackward (number:int) int{
return number-1
}
func choosestepfunction (Chooseflag:bool)-(Int)->int{
return Chooseflag? Gobackward:goforward
}
var countingnumber = 3 Let
movefunc = Choosestepfunction (chooseflag: Countingnumber > 0) while
countingnumber! = 0 {
print (String (countingnumber))
Countingnumber = Movefunc (Countingnumber)
}
print ("0!")
3 2 1 0!
nested Functions
All the functions you see are called global functions, which are defined in the global domain. You can also define functions in other function bodies, called nested functions (nested functions).
///nesting functions////////-Parameter Chooseflag: < #chooseFlag description#>//-Returns: < #ret Urn Value description#> func nestchoosestepfunction (Chooseflag:bool)--(INT)->int{func stepf (num:int)->i Nt{return num + 1} func STEPB (num:int)->int{return num-1} return Chooseflag? STEPF:STEPB} var countingnum = 5 Let Resultfunc = nestchoosestepfunction (Chooseflag:countingnum < 0) while Count
Ingnum < 0 {print ("\ (countingnum) +"! ")
Countingnum = Resultfunc (countingnum)} print ("0!")/*-5!
-4!
-3!
-2!
-1!
0! */