Function Definition in Swift
Level 3151 keystrokes
Defines a function with argument labels, a default parameter value and a String return type, and calls it with times: 2 to repeat the greeting twice.
Swift code
// times defaults to 1 if omitted; -> String is the return type1func greet(name: String, times: Int = 1) -> String { // String(repeating:count:) repeats the text times times (each copy ends with a space)2 return String(repeating: "Hello, \(name)! ", count: times)3}// Arguments are passed with their labels: Hello, Swift! Hello, Swift!4print(greet(name: "Swift", times: 2))Output
Hello, Swift! Hello, Swift!
Practice typing this code. Results are not recorded in the rankings.
Symbols in this code
The symbols you type in this Swift code and how often they appear.
:× 6(× 5)× 5,× 4"× 4-× 1!× 1{× 1}× 1\× 1