Swift: Multiple Parameters

Multiple Parameters in Swift:

A function, just like a program, can handle multiple types of data. We’ve only worked with one parameter per function so far, but Swift functions can now accept multiple parameters values separated by commas at the same time.

Consider a function that determines whether a programmer can advance to the next round of qualification based on their current performance in a competition. We can use multiple parameters in a function because this is dependent on two types of data the programmer’s knowledge and logic:

func qualify(knowledge: Bool, logic: Bool) -> String {
  if knowledge && logic {
    return "Eligible!";
  } else {
    return "Not Eligible!";
  }
}

Let’s take a look underneath the hood:

  • Within the function qualify(knowledge:logic:), which returns a String value, knowledge and logic are Bool parameters.
  • The parameters are used in an if/else statement to determine if the programmer progresses to the next round.

We pass true for knowledge and false for logic when calling the function below:

print(qualify(knowledge: true, logic: false));

So, the function with the Boolean values executes the else statement, and the function returns "Not Eligible!".

Helpful Tools:

Here are some useful tools to help you along your journey!

Setting up an IDE (Integrated Development Environment) can be difficult for beginners. The Online Compiler will enable you to run your code inside your browser without the need to install an IDE. If you need a more detailed explanation of a specific topic, the best place to find answers is in the Official Documentation.

Scroll to Top
%d bloggers like this: