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 :: corner radius with animation swift 
Swift :: How to Hide Password in Text field Swift 
Swift :: ternary operator in swift 
Swift :: Swift break statement with for loop 
Swift :: Arithmetic Operators in Swift 
Swift :: Example: Nested Tuple 
Swift :: property observer in swift 
Swift :: Swift Bitwise OR Operator 
Swift :: Convert struct to JSON string in swift 5 
Swift :: string to decimal swift 
Swift :: underline text in storyboard xcode 
Ruby :: How to find database name in rails console 
Ruby :: ruby raise error 
Ruby :: ruby remove unsafe file characters 
Ruby :: rspec check if object of a class 
Ruby :: in query postgres jsonb rails 
Ruby :: rails disable cache on dev 
Ruby :: max keys from hash ruby 
Ruby :: rails string to date 
Ruby :: ruby unshift method 
Ruby :: new date ruby 
Ruby :: ruby sort array numerically 
Ruby :: linked list in ruby 
Ruby :: rails generate controller no respec 
Ruby :: pg_ctl: no database directory specified and environment variable PGDATA unset 
Ruby :: Rails is not defined 
Ruby :: ruby string to boolean 
Ruby :: ruby rails migrate check status 
Ruby :: List columns in table from console 
Ruby :: activerecord exclude 
ADD CONTENT
Topic
Content
Source link
Name
5+2 =