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 :: parsing to double any data type in swift 
Swift :: create a dictionary in swift 
Swift :: Swift Access Class Property using Objects 
Swift :: Changing default url to static-media in Flask 
Swift :: swift UIColor to String 
Swift :: swift looping through array 
Swift :: Why Inheritance? 
Swift :: 95 dollars in rupees 
Swift :: how to get list of value from specific keys in array object in swift 
Swift :: Generic Function Swift 
Swift :: Swift Nested Function with Return Values 
Swift :: Swift Benefits of Using Functions 
Swift :: ns transport swift code 
Swift :: swift isMemberOf 
Swift :: Arithmetic Operators in Swift 
Swift :: print 1 line swift 
Swift :: uitableview disable sticky header 
Swift :: swift Equatable 
Ruby :: rails index name too long 
Ruby :: rails activestorage get image url 
Ruby :: how to check ruby version 
Ruby :: ruby replace first character in string 
Ruby :: Your Ruby version is 3.0.0, but your Gemfile specified 2.7.4 
Ruby :: ruby make chain method 
Ruby :: find a key in nested hash ruby 
Ruby :: for loop ruby 
Ruby :: ruby loop through array 
Ruby :: ruby get current process id 
Ruby :: read headers of csv using ruby 
Ruby :: rails image disappears after showing 
ADD CONTENT
Topic
Content
Source link
Name
4+4 =