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 :: how to change the tint color of tab bar on storyboard swift 
Swift :: Define Swift Structure 
Swift :: swift methods 
Swift :: Swift Benefits of Using Functions 
Swift :: swift ease in out animatekeyframes 
Swift :: swift dictionary to json string online 
Swift :: swift uitextfield only numbers keyboard lock programmatically 
Swift :: swift isMemberOf 
Swift :: swift computed property 
Swift :: Swift Literals 
Swift :: swift complete print function syntax 
Swift :: spilit string in swift 
Swift :: how to convert a url to string in swift 
Swift :: cellwidget to setvalue 
Ruby :: rails index name too long 
Ruby :: activerecord list tables 
Ruby :: rails get current database name 
Ruby :: rails migration rename column 
Ruby :: rails disable cache on dev 
Ruby :: head in rails 
Ruby :: merge two lists together ruby 
Ruby :: ruby hash merge 
Ruby :: rails activerecord to hash 
Ruby :: iterate through values of an object rails 
Ruby :: ruby latest version 
Ruby :: create_enum in rails 7 
Ruby :: ruby routes 
Ruby :: While executing gem 
Ruby :: ruby reduce hash 
Ruby :: ruby for programmers 
ADD CONTENT
Topic
Content
Source link
Name
2+1 =