Preface : Not long ago ,Swift The language has also been updated to 3.0 edition , A friend who has a certain foundation in programming will not be unfamiliar with the concept of function . and Swift The functions in languages are similar , Today, I will follow Xiao Bian to learn Swift3 Different uses of functions in .

1) Define a function and call

 // One , Define a function with no parameters and no return value 
func fun1(){
print("this is first function")
}
fun1()

2) Define a function with parameters and return values

 func fun2(a:Int) ->Int{
print("this is second function")
return a
}
// Function call
fun2(a: )

analysis : The parameters of the function are placed in parentheses after the function name , Multiple parameters separated by commas , The return value type is indicated by a connector and a right angle bracket (->)

3) The calling function must specify the parameter name and the parameter order cannot be changed

 func add1(firstNum:Int,addWithNum:Int) ->Int{
return firstNum+addWithNum
}
// Error case one 、add1(2,3) The calling function must specify the parameter name
// Error case 2 、add1(addWithNum: 4 ,firstNum:
add1(firstNum: , addWithNum: )

4) When using underscores to omit the specified parameter name when calling a function , The parameter name must be omitted when calling

 func add2(_ firstNum:Int,_ addWithNum:Int) ->Int{
return firstNum+addWithNum
}
// Wrong cases add2(2, addWithNum: 4)
add2(, )

5) Specify external parameters when defining functions

 func add3(a firstNum:Int,b addWithNum:Int) ->Int{
return firstNum + addWithNum
}
add3(a: , b: )// When called, specify the external parameter name

6) When declaring external parameters , You can't use external arguments inside a function , At the same time, internal parameter assignment cannot be used when calling

 // Wrong cases 
//func add4(a firstNum:Int,b addWithNum:Int) ->Int{
// return a + b value
//}
//add4(firstNum: 2, addWithNum: 4)

7) In the case of external parameters , You can't use underscores to omit parameters

 // Wrong cases 
//func add5(_ a firstNum:Int,b addWithNum:Int) ->Int{
// return firstNum + addWithNum
//}

8) When defining a function with parameters, you can set a default value for the parameters

 func add5(firstNum:Int,addWithNum:Int = ) ->Int{
return firstNum+addWithNum
} add5(firstNum:) // Get the results 6
add5(firstNum: ,addWithNum: )// Get the results 10

9) Function in Swift It's the first class in language ( Be similar to Java Class in ), It means that it can be a function parameter and a return value

 func outer4(f:()->Void) -> (Int,Int)->(Int,Int) {
//......
}

Swift3 More articles about the use of functions in

  1. Swift A guide to the use of functions in

    About Swift Summary of the use of various functions in Preface Time is long. , A lot of things we will slowly forget , Here's a summary Swift The principle of using functions in , Most of the function using skills are demonstrated with code examples , But if you want to improve , We have to think a lot ...

  2. How to understand javaSript The parameters of functions in are passed by value

    This article is based on the Hongbao book <Javascript Advanced programming > Chapter four of ,4.1.3 Passing parameters section P70, Further understanding javaSript The parameters of the function in , How to pass parameters when they are objects . ( Individuals who combine information ...

  3. JavaScript Function definition and variable declaration in &lt; Basic knowledge 1 &gt;

    1.JavaScript There are three ways to construct functions in a.function createFun(){ } b.var createFun=function (){ } c.var createFun=new ...

  4. oracle The differences and relations between functions and stored procedures in 【 Reprinted bamboo Pinellia 】

    oracle The differences and relations between functions and stored procedures in [ Reprinted bamboo Pinellia ] stay oracle in , Functions and stored procedures are often used , There are many similarities in their grammar , But it has its own characteristics . Just finished learning functions and stored procedures , Let's share with you ...

  5. string.h Function usage in file

    The following is string.h Detailed usage of functions in the file : strcpy Function name :strcpy work can : Copy one string to another with Law : char *strcpy(char *destin, char *source) ...

  6. Three languages (c++、as、lua) The difference of the function in

    For different languages , Especially static language and dynamic language , For the definition of function ( How to look at a function ) It's very different from dealing with . Specifically, it can be divided into two categories : 1. Think of functions as first type values , That is, functions are the same as other objects , It's a common object in language ...

  7. C/C++ Function parameter transfer details ( Two )

    Yesterday, I read about memory management , Have a little understanding of , But not very deep , I found that I didn't pay attention to many details when I wrote the code before , Only know that this can achieve the function , But I don't know why , The hidden danger caused by adopting one's own method is also unknown , I don't know if there are other parties ...

  8. Dynamic link library function address determination ---PLT and GOT [ turn ]

    I've written about DLL before A blog post on delayed binding , I like that article very much , But it was just clear , My writing is messy , I recently learned Ulrich Drepper Of How to write share library, I learned a few ...

  9. Javascript Four ways to call functions in

    One .Javascript Several basic knowledge points of functions in : 1. The name of a function is just a pointer to a function , So even in different execution environments , That is, different objects call this function , This function still points to the same function . 2. There are two special internal genera in functions ...

Random recommendation

  1. Maven from svn After downloading ,pom.xml Error reporting solutions

    Multiple annotations found at this line: - Execution default-testResources of goal org.apache.maven. ...

  2. I found one at the weekend BUG, Sometimes there is no , The program crashes as soon as it comes out , Depressed for a long time , Finally, I followed , Remember , Remind yourself to standardize the compilation in the future , As long as the standard , There will never be a problem

    -- :::] cell0 create -- :::] *** Assertion failure /UITableView.m: -- :::] [ Uncaught Exception ] Na ...

  3. windows Upper use image library

    First, install the library , have access to pip install , So we need to first install pip Go to https://bootstrap.pypa.io/get-pip.py download get-pip.py, And then run python get-pip ...

  4. LeetCode Kth Largest Element in an Array ( Quick sort )

    The question : In an unordered array k What's the big number ? Ideas : According to the idea of the fast line , If it's divided into two parts at a time , Set to L and R. If R>=k , The answers are set on the right , Otherwise gather on the left . This is used here. 3 Take the middle . Pay attention to quick line up, don't write dead loop ...

  5. ( primary + turn ) Use opencv Of DFT Compute convolution

    Reprint please indicate the source : http://www.cnblogs.com/darkknightzh/p/5462665.html Refer to the website : http://blog.csdn.net/lichengyu/art ...

  6. IOS Develop common basic methods

    .// Exit the keyboard [self.view endEditing:YES]; Hide the status bar above the phone -(BOOL)prefersStatusBarHidden{ return YES; } // Get the current control ...

  7. 2018-2019 ACM-ICPC Nordic Collegiate Programming Contest (NCPC 2018) - 4.28

    I made up a few after the game I wrote two in the competition ... A - Altruistic AmphibiansGym - 101933A After looking at the list, few people do it . I didn't see it . It turns out that there's one DP( But I think complexity is a bit of a mystery ) The question : ...

  8. Inquire about SQL Server Executed SQL sentence

    SELECT TOP 1000         ST.text AS ' Executive SQL sentence ',        QS.execution_count AS ' Number of executions ',        QS.total_ ...

  9. 【BZOJ3894】【Luogu3358】 Arts and Sciences Division - The first mock exam model

    link Click Here This is the first mock exam of a board. \(QwQ\), A general solution is introduced here . Put aside portfolio income , This problem is a simple minimal cut model . We just need to build a map like this , Run on it , cut ...

  10. request Set up cookies

    mycookie = { "PHPSESSID":"56v9clgo1kdfo3q5q8ck0aaaaa" } request.get(url,cookies ...