Finish Functions

This commit is contained in:
Gary Gan 2025-01-21 10:57:56 +08:00
parent 52f7894a8d
commit 03cf181454
3 changed files with 67 additions and 0 deletions

View File

@ -0,0 +1,62 @@
import Foundation
func noArgumentAndNoReturnValue() {
"I don't know what I'm doing"
}
noArgumentAndNoReturnValue()
func plusTwo(value: Int) {
let newValue = value + 2
}
plusTwo(value: 30)
func newPlusTwo(value: Int) -> Int {
return value + 2
}
newPlusTwo(value: 30)
func customAdd(value1: Int, value2: Int) -> Int {
value1 + value2
}
let customAdded = customAdd(value1: 10, value2: 30)
// function(external internal: Type) -> Type
func customMinus(_ lhs: Int, _ rhs: Int) -> Int {
lhs - rhs
}
customMinus(10, 2)
customAdd(value1: 20, value2: 60)
@discardableResult
func myCustomAdd(_ lhs: Int, _ rhs: Int) -> Int {
lhs + rhs
}
func doSomethingComplicated(with value: Int) -> Int{
func mainLogic(value: Int) -> Int {
value + 2
}
return mainLogic(value: value + 3)
}
doSomethingComplicated(with: 10)
func getFullName(
firstName: String = "Foo",
lastName: String = "Bar"
) -> String {
"\(firstName) \(lastName)"
}
getFullName()
getFullName(firstName: "Gary")
getFullName(lastName: "Gan")
getFullName(firstName: "Gary", lastName: "Gan")

View File

@ -0,0 +1,2 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<playground version='7.0' target-platform='macos' swift-version='6' buildActiveScheme='true' executeOnSourceChanges='true' importAppTypes='true'/>

View File

@ -1,6 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<Workspace
version = "1.0">
<FileRef
location = "group:Functions.playground">
</FileRef>
<FileRef
location = "group:Variables.playground">
</FileRef>