Menu
Coddy logo textTech

Parameters And Arguments

Part of the Fundamentals section of Coddy's Swift journey — lesson 52 of 86.

Functions become much more powerful when they can accept input. Parameters are placeholders defined in the function declaration, while arguments are the actual values you pass when calling the function.

To add a parameter, specify its name and type inside the parentheses:

func greet(name: String) {
    print("Hello, \(name)!")
}

greet(name: "Alice")  // Output: Hello, Alice!
greet(name: "Bob")    // Output: Hello, Bob!

Here, name: String is the parameter—it tells Swift the function expects a String value. When calling the function, you provide the argument using the parameter name followed by a colon.

Functions can accept multiple parameters, separated by commas:

func introduce(name: String, age: Int) {
    print("\(name) is \(age) years old.")
}

introduce(name: "Sara", age: 25)
// Output: Sara is 25 years old.

Each parameter acts like a constant inside the function body—you can use it but not modify it. The order of arguments must match the order of parameters in the function definition.

challenge icon

Challenge

Easy
Write a function createFullName that takes firstName and lastName and returns the full name as a single string.

Combine the two names with a space between them.

Parameters:

  • firstName (String): The person's first name
  • lastName (String): The person's last name

Returns: The full name with a space between first and last name (String)

For example, if firstName is "John" and lastName is "Doe", the function returns "John Doe".

Cheat sheet

Functions can accept input through parameters. Parameters are placeholders defined in the function declaration with a name and type:

func greet(name: String) {
    print("Hello, \(name)!")
}

greet(name: "Alice")  // Output: Hello, Alice!

When calling a function, you pass arguments using the parameter name followed by a colon.

Functions can accept multiple parameters, separated by commas:

func introduce(name: String, age: Int) {
    print("\(name) is \(age) years old.")
}

introduce(name: "Sara", age: 25)
// Output: Sara is 25 years old.

Parameters act like constants inside the function body and cannot be modified. The order of arguments must match the order of parameters.

Try it yourself

func createFullName(firstName: String, lastName: String) -> String {
    // Write code here
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals