This time, we learned about functions, so we will output them.
*** A function is a function that combines statements that are defined in multiple places in advance into one. *** ***
The basic function declaration is as follows.
qiita.rbvar
func <Function name>(){
<Sentence>
}
Write "func" at the beginning like this. This is an abbreviation for "function" in English, which means function.
Let's look at a simple example next!
qiita.rbvar
func kuku5dan(){
for x in 1...9{
print(5*x)
}
}
kuku5dan() //Declare like this to do
Execution result
5
10
15
20
.
.
The declaration of the function that used the argument is as follows.
qiita.rbvar
func <Function name>(<Argument name>:<Type>){
<Sentence>
}
Let's look at a simple example next.
qiita.rbvar
func kuku(num:Int) {
for x in 1...9{
print(num*x)
}
}
kuku(num:5) //5 steps execution
qiita.rbvar
func <Function name>(<Argument name 1>:<Type 1>,<Argument name 2>:<Type 2>){
<Sentence>
}
It can be easily separated by "," as described above.
Let's look at a simple example here as well.
qiita.rbvar
func rectangleArea(height:Int,width:Int){
print(height*width)
}
ectangleArea(height: 3, width:4) //Function call
In the above example, the expression for calculating the area of the rectangle is declared as a function and multiple arguments are specified.
The argument can be labeled before the argument name. The reason for specifying the label is to make the sentence as natural as possible in English and to make it easier for people who have not written the code to see and understand it.
Let's look at the basic structure.
qiita.rbvar
func <Function name>(<Label 1><Argument name 1>:<Type 1>,<Label 2><Argument name 2>:<Type 2>){
<Sentence>
}
In this way, you can easily specify the label before the argument name.
The functions explained above have been completely processed within the functions. However, depending on the content of the process, you may want to use the process performed by the function to perform another process. The *** return value *** is used at this time.
To define a return value for a function, write "->" after the function name to specify the return type, as shown below. Also, specify the actual value to return after the return keyword.
Let's look at the basic structure!
qiita.rbvar
func <Function name>() -> <Return value type>{
<Sentence>
return<Return value>
}
Let's look at a simple example next.
qiita.rbvar
func rectangleArea(height:Int,width:Int) -> Int{
let result = height*width
return result //Return the calculation result
}
var area = rectangleArea(height:5,width:6) //The execution result of the function is assigned to the variable area
print(area)
Finally, I will summarize what I learned this time. -Functions can be used to combine duplicate code into one -To use a function, two steps are required: declaring the function and calling the function. -Using arguments, you can combine similar functions into one. -Multiple values can be specified for the argument ・ By specifying the label, the code becomes easy to read. -If you want to use the processing result of the function in other code, set the return value.
Functions play a very important role in app development, so we will deepen our understanding.
Recommended Posts