Let's understand the function!

This time, we learned about functions, so we will output them.

What is a function?

*** 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
.
.

Function declaration with arguments

● What is an argument? Arguments are the values you pass to a function when it is executed. The function can be executed using that argument.

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

● Multiple arguments The function can accept multiple arguments.

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.

Label designation

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.

Function return value definition

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

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

Let's understand the function!
Let's understand the if statement!
Let's understand the guard statement!
Let's understand the for-in statement!
Let's understand the switch statement!
Let's understand the Array (Element) type!
Let's understand the Optional (Wrapped) type!
Let's understand closures!
About the function double-java
Let's introduce the credit card function using payjp (preparation)
Let's make the app better
Understand the basics of docker
Let's try the S2Struts tutorial (# 3_180425)
Let's solve the roman numerals
Understand the helper method form_with
Let's try the S2Struts tutorial (# 5_180526)
Rails ~ Understanding the message function ~
Let's try the S2Struts tutorial (# 4_180505)
Let's try the S2Struts tutorial (# 1_180423)
Let's solve the FizzBuzz problem!
Let's try the S2Struts tutorial (# 2_180424)
Follow function association memorandum (understand the description of the User model)
Let's roughly implement the image preview function with Rails + refile + jQuery.
Understand the basic mechanism of log4j2.xml
[java8] To understand the Stream API
[Rails] About the Punk List function
User evaluation using the like function
Let's verify the image search filter
Understand the difference between each_with_index and each.with_index
Let's try the S2Struts tutorial (# 0_yymmdd)
Yes, let's preview the image. ~ part5 ~
How to add the delete function
Image processing: Let's play with the image
Let's attack the vulnerability (2) Open redirect
Let's specify the version when using docker
Let's briefly explain the portfolio we created.
Understand the basics of Android Audio Record
Java reference to understand in the figure
[Java] How to use the hasNext function
Now let's recap the Java lambda expression
Understand the official sample Coffee of Dagger2
[Swift] How to implement the countdown function
Where the follow function implementation is stuck
Let's analyze the GC log using GCMV
[Processing × Java] How to use the function
[Ruby] What is the slice method? Let's solve the example and understand the difference from slice!
[Swift, a must-see for fledgling! ] Let's understand the implementation of communication processing of WebAPI