Skip to content

Instantly share code, notes, and snippets.

@alexpaul
Last active September 10, 2020 02:18
Show Gist options
  • Save alexpaul/d1d8059c9eaa286112bca2ca7e4f810b to your computer and use it in GitHub Desktop.
Save alexpaul/d1d8059c9eaa286112bca2ca7e4f810b to your computer and use it in GitHub Desktop.
Using `readLine()` to take in STDIN (input) from the user or command line and printing to the console (STDOUT).
import Foundation 

func add(a: Int, b: Int) -> Int {
  return a + b 
}

while true {
  print("enter a")
  let a = Int(readLine() ?? "") ?? 0

  print("enter b")
  let b = Int(readLine() ?? "") ?? 0

  let result = add(a: a, b: b)
  print("the result is \(result)")
}

Other Resources

  1. Using STDIN and STDOUT
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment