How to define and call functions in swift?
Example:
func greet(name: String) -> String {
return "Hello, (name)!"
}
print(greet(name: "ChatGPT"))
This function takes a name as an argument and returns a greeting message.
Solution:
func greet(name: String, times: Int) -> String {
var message = ""
for _ in 1...times {
message += "Hello, (name)! "
}
return message
}
print(greet(name: "ChatGPT", times: 3))
This solution modifies the function to repeat the greeting a specified number of times.