Bird
Raised Fist0
Swiftprogramming~20 mins

Function declaration syntax in Swift - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Swift Function Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of a simple Swift function
What is the output of this Swift code?
Swift
func greet(name: String) -> String {
    return "Hello, \(name)!"
}

print(greet(name: "Anna"))
Agreet(name: "Anna")
BError: Missing return statement
CHello, name!
DHello, Anna!
Attempts:
2 left
💡 Hint
Look at how the function returns a string with the name inserted.
❓ Predict Output
intermediate
2:00remaining
Function with multiple parameters
What does this Swift function print?
Swift
func multiply(_ a: Int, _ b: Int) -> Int {
    return a * b
}

print(multiply(3, 4))
A7
B12
C34
DError: Missing argument labels
Attempts:
2 left
💡 Hint
The function multiplies two numbers and returns the result.
❓ Predict Output
advanced
2:00remaining
Function with default parameter values
What is the output of this Swift code?
Swift
func greet(name: String = "Guest") {
    print("Welcome, \(name)!")
}

greet()
greet(name: "Sam")
AWelcome, Guest!\nWelcome, Sam!
BWelcome, !\nWelcome, Sam!
CWelcome, Guest!\nWelcome, Guest!
DError: Missing argument for parameter 'name'
Attempts:
2 left
💡 Hint
Default parameters are used when no argument is provided.
❓ Predict Output
advanced
2:00remaining
Function with inout parameter
What is the value of x after this Swift code runs?
Swift
func increment(value: inout Int) {
    value += 1
}

var x = 5
increment(value: &x)
x
A6
BError: Cannot pass immutable value as inout argument
C5
DError: Missing & before argument
Attempts:
2 left
💡 Hint
inout allows the function to modify the original variable.
❓ Predict Output
expert
3:00remaining
Function with multiple return values using tuple
What does this Swift code print?
Swift
func minMax(numbers: [Int]) -> (min: Int, max: Int)? {
    guard !numbers.isEmpty else { return nil }
    var currentMin = numbers[0]
    var currentMax = numbers[0]
    for number in numbers[1...] {
        if number < currentMin {
            currentMin = number
        } else if number > currentMax {
            currentMax = number
        }
    }
    return (currentMin, currentMax)
}

if let result = minMax(numbers: [3, 7, 2, 9, 4]) {
    print("Min is \(result.min), Max is \(result.max)")
} else {
    print("Empty array")
}
AMin is 9, Max is 2
BMin is 3, Max is 7
CMin is 2, Max is 9
DEmpty array
Attempts:
2 left
💡 Hint
The function finds the smallest and largest numbers in the array.

Practice

(1/5)
1. Which keyword is used to declare a function in Swift?
easy
A. def
B. func
C. function
D. method

Solution

  1. Step 1: Recall Swift function declaration

    In Swift, functions always start with the keyword func.
  2. Step 2: Compare options

    Only func uses func, the correct keyword for Swift functions.
  3. Final Answer:

    func -> Option B
  4. Quick Check:

    Swift functions start with func [OK]
Hint: Remember: Swift functions always start with 'func' keyword [OK]
Common Mistakes:
  • Using 'function' instead of 'func'
  • Confusing Swift with Python's 'def'
  • Using 'method' which is not a keyword
2. Which of the following is the correct syntax to declare a function named greet that takes no parameters and returns nothing?
easy
A. func greet() -> void {}
B. function greet() {}
C. func greet {}
D. func greet() {}

Solution

  1. Step 1: Check function declaration syntax

    In Swift, a function with no parameters and no return type is declared as func name() {}.
  2. Step 2: Analyze each option

    func greet() {} matches the correct syntax. function greet() {} uses 'function' which is invalid. func greet {} misses parentheses. func greet() -> void {} uses 'void' which is not used in Swift.
  3. Final Answer:

    func greet() {} -> Option D
  4. Quick Check:

    Empty parameter list needs () and no return type means no arrow [OK]
Hint: Empty parameters always need () after function name [OK]
Common Mistakes:
  • Omitting parentheses for parameters
  • Using 'function' instead of 'func'
  • Using 'void' as return type in Swift
3. What will be the output of the following Swift code?
func add(a: Int, b: Int) -> Int {
    return a + b
}

print(add(a: 3, b: 4))
medium
A. a + b
B. 34
C. 7
D. Error: Missing return statement

Solution

  1. Step 1: Understand function parameters and return

    The function add takes two integers and returns their sum.
  2. Step 2: Calculate the function call result

    Calling add(a: 3, b: 4) returns 3 + 4 = 7, which is printed.
  3. Final Answer:

    7 -> Option C
  4. Quick Check:

    Sum of 3 and 4 is 7 [OK]
Hint: Add numbers inside function and print result [OK]
Common Mistakes:
  • Concatenating numbers as strings
  • Forgetting to return a value
  • Misunderstanding parameter labels
4. Identify the error in the following Swift function declaration:
func multiply(a: Int, b Int) -> Int {
    return a * b
}
medium
A. Missing colon between parameter name and type for b
B. Missing return keyword
C. Function name is invalid
D. Return type should be String

Solution

  1. Step 1: Check parameter syntax

    Each parameter must have a colon between its name and type. Here, b Int is missing a colon.
  2. Step 2: Confirm other parts are correct

    The return keyword is present, function name is valid, and return type Int is appropriate.
  3. Final Answer:

    Missing colon between parameter name and type for b -> Option A
  4. Quick Check:

    Parameters need colon between name and type [OK]
Hint: Check colons between parameter names and types [OK]
Common Mistakes:
  • Omitting colon in parameter list
  • Forgetting return keyword
  • Using wrong return type
5. You want to declare a Swift function named describePerson that takes a String parameter name and an Int parameter age, and returns a String describing the person like "Name is Age years old." Which is the correct function declaration?
hard
A. func describePerson(name: String, age: Int) -> String { return "\(name) is \(age) years old." }
B. func describePerson(name String, age Int) -> String { return "\(name) is \(age) years old." }
C. func describePerson(name: String, age: Int) { print("\(name) is \(age) years old.") }
D. func describePerson(name: String, age: Int) -> Void { return "\(name) is \(age) years old." }

Solution

  1. Step 1: Check parameter syntax and return type

    Parameters must have colons between name and type. The function returns a String, so return type must be String.
  2. Step 2: Verify function body matches return type

    func describePerson(name: String, age: Int) -> String { return "\(name) is \(age) years old." } returns a String with correct string interpolation. func describePerson(name String, age Int) -> String { return "\(name) is \(age) years old." } misses colons. func describePerson(name: String, age: Int) { print("\(name) is \(age) years old.") } has no return type and uses print instead of return. func describePerson(name: String, age: Int) -> Void { return "\(name) is \(age) years old." } uses Void return type but returns a String, which is invalid.
  3. Final Answer:

    func describePerson(name: String, age: Int) -> String { return "\(name) is \(age) years old." } -> Option A
  4. Quick Check:

    Correct parameters and return type with string interpolation [OK]
Hint: Use colons for parameters and match return type with returned value [OK]
Common Mistakes:
  • Omitting colons in parameters
  • Using print instead of return for returning values
  • Mismatching return type and returned value
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard