1 Answer

Default Parameter Values

You can define a default value for any parameter as part of a function’s definition. If a default value is defined, you can omit that parameter when calling the function.

NOTE

Place parameters with default values at the end of a function’s parameter list. This ensures that all calls to the function use the same order for their non-default arguments, and makes it clear that the same function is being called in each case.

Here’s a version of the join function from earlier, which provides a default value for its joiner parameter:

funcjoin(strings1: String, toStrings2: String,

withJoinerjoiner: String = " ") -> String {

returns1 + joiner + s2

}

If a string value for joiner is provided when the join function is called, that string value is used to join the two strings together, as before:

join(string: "hello", toString: "world", withJoiner: "-")

// returns "hello-world"

Your comment on this answer:

Your name to display (optional):

Email me at this address if a comment is added after mine:Email me if a comment is added after mine

Privacy: Your email address will only be used for sending these notifications.