6.2. Functions with parameters

Now, let's write a function that accepts parameters and do some tasks. The following code accepts the name and age of a person as parameters and prints the details.

//function definition...
func printDetails(name:String, age:Int){
    print(
        """
        The user's name is: \(name).
        The user's age is: \(age).
        \(name) is awesome!
        """
    )
}


//calling the function...
printDetails(name: "Donald", age: 25)

It prints:

The user's name is: Donald.
The user's age is: 25.
Donald is awesome!

Last updated

Was this helpful?