swift-playground/Functions.playground/Contents.swift
2025-01-21 10:57:56 +08:00

63 lines
1.1 KiB
Swift

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")