6.4. More on function parameters

Parameter labels

Swift functions have the ability to have completely separate internal and external parameter names. Instead of having a single parameter name, the function below has two. The first one, with is an external parameter name, which is the name that will refer to the parameter when we call the function. The second one, vehicle is the internal parameter name which is the name we’ll use when we need to use the parameter within the function.

//function definition...
func navigate(with vehicle:String, from source:String, to destination:String) -> String{
    return "The user will use a \(vehicle) to travel from \(source) to \(destination)."
}

//calling the function
print(navigate(with: "car", from: "Boston", to: "NYC"))

It prints:

The user will use a car to travel from Boston to NYC.

Let's look at the first String parameter the navigate function accepts. Here, with is the external name or label, and vehicle is the internal label for the first parameter. When we are calling the function from outside, we are using with, and internally we are using vehicle.

We can use this feature to write and call a function creatively.

Omitting parameter labels

We can also omit the external parameter name to call our function just by passing in a value without mentioning the parameter's name. We achieve this by simply replacing the external name with the underscore. Now we can call our function without using a parameter name at all. Instead, we can pass in a value that we want to use as the input.

Remember underscores? We can use it to define the external name of a parameter to omit the need to type the parameter name when we call the function:

//function definition...
func printHello(_ name:String){
    print("Hello \(name)!")
}

//Calling the function...
printHello("Sakib")

Default parameters

Sometimes we want to have default values for our parameters. The default value gets activated if we do not pass any value through the parameters. We can set a default value by writing a = after the parameter's type followed by the default value. Like this:

It will print:

Because the default is already set to "Unknown."

Why and where do we use internal and external names in Swift functions?

  1. Improved Clarity in Function Calls:

    When a function has descriptive external parameter names, it becomes clear what each argument represents when calling it. This helps understand each argument's purpose and makes the function call more readable.

  2. Self-Documenting Code:

    By providing meaningful external names, you can make your code self-documenting. This means that someone reading your code can understand the purpose and intent of the function just by looking at the function signature and the way it's called.

  3. Avoiding Ambiguity: In some cases, functions may have parameters with similar data types. Using external names allows you to disambiguate these parameters, making the function call unambiguous and reducing the risk of passing incorrect arguments.

  4. Improved Readability and Maintenance:

    By using external names, you can create functions with expressive, almost sentence-like function calls, which can be beneficial when reading and maintaining code.

Scenarios where internal and external names are useful

  1. Public API Design:

    When designing public APIs, using external names helps ensure that the function calls are clear and self-explanatory for your framework or library users. You can find examples in Apple Documentation.

  2. Functions with Multiple Parameters:

    Functions with multiple parameters benefit from external names as they clarify the purpose of each parameter.

  3. Methods in Classes and Structures:

    In object-oriented programming, methods in classes and structures should use external names to indicate the role of each parameter in the context of the object.

In summary, using internal and external names in Swift functions is a good practice for writing clean, expressive, and self-documenting code, especially when designing public APIs or functions with multiple parameters. They enhance code readability and help prevent confusion and errors when calling functions.

These are the very basics of functions in Swift. Eventually, we will learn more about functions and their various uses.

Last updated

Was this helpful?