From 03cf18145479e929dc329cb787db74ea5815ff25 Mon Sep 17 00:00:00 2001 From: Gary Gan Date: Tue, 21 Jan 2025 10:57:56 +0800 Subject: [PATCH] Finish Functions --- Functions.playground/Contents.swift | 62 +++++++++++++++++++ Functions.playground/contents.xcplayground | 2 + .../contents.xcworkspacedata | 3 + 3 files changed, 67 insertions(+) create mode 100644 Functions.playground/Contents.swift create mode 100644 Functions.playground/contents.xcplayground diff --git a/Functions.playground/Contents.swift b/Functions.playground/Contents.swift new file mode 100644 index 0000000..05db1e5 --- /dev/null +++ b/Functions.playground/Contents.swift @@ -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") + diff --git a/Functions.playground/contents.xcplayground b/Functions.playground/contents.xcplayground new file mode 100644 index 0000000..10de914 --- /dev/null +++ b/Functions.playground/contents.xcplayground @@ -0,0 +1,2 @@ + + \ No newline at end of file diff --git a/swift-playground.xcworkspace/contents.xcworkspacedata b/swift-playground.xcworkspace/contents.xcworkspacedata index 3264df6..b6c22fe 100644 --- a/swift-playground.xcworkspace/contents.xcworkspacedata +++ b/swift-playground.xcworkspace/contents.xcworkspacedata @@ -1,6 +1,9 @@ + +