function nested invocation form of Swift2.0 language tutorialSwift2.0 languagefunction nested invocation form
in the Swift , a function can also be called in a function to form a nested call. Nested calls tend to have two forms: one is to call other functions in one function, and the other is to call a function in a function. These two calls are explained in more detail below.
Swift2.0 languagenested invocation Basic form
nested calls refer to other functions that can be called in a function. The invocation method is shown in 7.4.
Figure 7.4 form of function nesting
The example 7-25 the following will use nested calls to the function to implement the s = 22! + 32! the calculation. The code is as follows:
Import Foundation
Func F1 (P:int)->int{
var k:int
var r:int
-
& nbsp k=p*p // squared
-
& nbsp r=f2 (k) // call function f2 ()
return r// gets the value of the factorial after the square
}
// find factorial
Func F2 (q:int)->int{
var c:int=1
var i:int
For i=1;i<=q;++i{
C=c*i
}
return C; Get the value of factorial
}
var i:int
var s:int=0
For i=2;i<=3;i++ {
s=s+f1 (i);
}
Print ("s=\ (s)")
in this code, the first calculation22!this value, which is calledF1 ()function, calculating A, the result is4, and then in the callF2 ()function, for4the factorial is obtained, the calculation is completed22!, the result is assigned to thes, and then calculate32!the result, process and demand22!is the same, and finally the result of the calculation andsto Add. The results of the operation are as follows:
of the Swift2.0 languageRecursive invocation
a recursive invocation is a special case of nested calls, which can also be called recursion. It calls the function itself in the process of calling the function. "There used to be a mountain, there was a temple in the mountain, there was an old monk in the temple telling stories to the little monk, what was it about?" There used to be a mountain, there was a mountain ... "This is a typical recursion that can be cycled indefinitely. But recursion in the Swift language must have a condition that satisfies the end. The recursive invocation is shown in the form 7.5 .
Figure 7.5 Recursive invocation
The example7-26The following will be implemented by recursive invocation6The age of the individual. Section6The individual said he was more than the first5Personal Big3year, first5The individual said he was more than the first4Personal Big3year, first4The individual said he was more than the first3Personal Big3years, and so on, to reach the last person he said he was -years. The code is as follows:
Import Foundation
// seeking Age
Func calculatedage (n:int)->int{
var x:int
If n==1{
X=13
}else{
x=calculatedage (n-1) +3// Implementing recursion
}
return x
}
Let Age=calculatedage (6)
Print (age)
It's called process 7.6 is shown.
Figure 7.6 Execution Flow Graph
ends a recursive call when a recursive call satisfies a certain condition, at which pointx=13, i.e.calculatedage (1)to be -; Callcalculatedage (1) +3implementation of article2the calculation of individual age,calculatedage (2)to be -, type in sequence, and finally implementCalculatedage (6)calculation of age. The results of the operation are as follows:
Note: In writing a recursive implementation you need to be aware of the following 2 Point
q find the correct recursive algorithm
q to determine the end condition of the recursive algorithm, this is the key to determine whether the recursive program can end normally.
This article is selected from: Swift2.0 Language Quick Start v3.0 University bully Internal information, reproduced please indicate the source, respect the technology respect it people!
function nested invocation form of Swift2.0 language tutorial