Search
 
SCRIPT & CODE EXAMPLE
 

SWIFT

Swift Nested function

// outer function
func greetMessage() {

  // inner function
  func displayName() {
    print("Good Morning Abraham!")
  }

  // calling inner function
  displayName()
}

// calling outer function
greetMessage()
Comment

Swift Syntax of Nested Function

// outer function
func function1() {
  // code

  // inner function
  func function2() {
    // code
  }

}
Comment

Swift Nested Function with Parameters

// outer function
func addNumbers() {
  print("Addition")

  // inner function
  func display(num1: Int, num2: Int) {
      print("5 + 10 =", num1 + num2)
  }

  // calling inner function with two values
  display(num1: 5, num2: 10)
}

// calling outer function
addNumbers()
Comment

Swift Nested Function with Return Values

func operate(symbol: String) -> (Int, Int) -> Int {

  // inner function to add two numbers 
  func add(num1:Int, num2:Int) -> Int {
    return num1 + num2
  }

  // inner function to subtract two numbers    
  func subtract(num1:Int, num2:Int) -> Int {
    return num1 - num2
  }

  let operation = (symbol == "+") ?  add : subtract
  return operation
}

let operation = operate(symbol: "+")
let result = operation(8, 3)
print("Result:", result)
Comment

PREVIOUS NEXT
Code Example
Swift :: swift add navigation bar 
Swift :: date format swift 
Swift :: reprobate 
Swift :: how to recieve hex value from NSData swift 
Swift :: swift string format double 
Swift :: swift double v float 
Swift :: replace back button image swift 
Swift :: swift int to int32 
Swift :: polymorphism in swift 
Swift :: uikit call swiftui view 
Swift :: swift array map example 
Swift :: swiftui refresh view 
Swift :: swift go to main thread 
Swift :: Swift Floating-point Literals 
Swift :: UINavigationBar turns black 
Swift :: rotate sfsymbol horizontal flip swiftui 
Swift :: Swift Conform Class To Swift Protocol 
Swift :: Swift Library Function 
Swift :: swiftui calendar 
Swift :: convert meter to miles swift 
Swift :: Allow user to import image 
Swift :: ns transport swift code 
Swift :: caseiterable swift 
Swift :: So, because promart depends on both flutter_test from sdk and freezed ^1.1.1, version solving failed. [ ] FINE: Exception type: SolveFailure 
Swift :: declare empty dictionary in swift 
Ruby :: ruby delete file 
Ruby :: rails validate uniqueness 
Ruby :: ruby replace certain character 
Ruby :: add references rails migration 
Ruby :: merge two lists together ruby 
ADD CONTENT
Topic
Content
Source link
Name
9+3 =