Start learning free

LESSON 37 OF 415 · FUNCTIONS I

Parameters in Swift: Fixing a Misspelled Variable Name

Practice Read the full explanation

Inside the code editor we've tried to define and call a function that takes a number as argument and prints the double of that number to the console.

So when we ran the code, we expected it to print:

20

but it seems like we made some mistakes because when we run our code we get an error instead.


Assignment:

Your task is to fix our code such that no errors will be produced and it will print the desired output.

STEP 1 OF 1
    Ctrl + Enter to check

    SWIFT · FUNCTIONS I

    Buggy Code Non Existing Variable in Swift, explained

    What the code is supposed to do

    The function printDouble takes one number and prints twice its value. Calling printDouble(10) should print 20. The code you start with doesn't compile, so nothing is printed at all:

    func printDouble(_ n: Int) {
        print(2 * num)
    }
    
    func mainFunction() {
        printDouble(10)
    }
    

    The compiler reports this error:

    error: cannot find 'num' in scope
    

    What goes wrong

    A function parameter is a variable that exists only inside the function body. The header func printDouble(_ n: Int) creates a parameter named n. Each time the function is called, n receives the value of the argument, so for printDouble(10) it holds 10.

    The body then uses a name, num, that was never declared. It isn't a parameter, it isn't a local variable, and no global called num exists. Swift checks every name before it builds the program, so it stops with "cannot find 'num' in scope". "In scope" means "visible from this place in the code"; here, nothing called num is visible.

    The underscore in the header

    The _ before n is easy to misread. A Swift parameter can have two names: an argument label that callers write, and an internal name that the body uses. In _ n: Int, the underscore means "no label", so the call is printDouble(10) and not printDouble(n: 10). The name after the underscore, n, is the only name the body can use.

    Fixing it step by step

    1. Read the error. It names the identifier the compiler couldn't find: num.
    2. Find where it is used. It appears in the print line inside printDouble.
    3. Compare it with the header. The parameter is called n, not num.
    4. Make the two agree. The simplest fix is to use n in the body. The other fix is to rename the parameter to _ num: Int and leave the body alone. Both work, because only the match matters.

    The fixed code in Swift

    func printDouble(_ n: Int) {
        print(2 * n)
    }
    
    func mainFunction() {
        printDouble(10)
    }
    

    A few details about this code:

    • Names are case-sensitive. N and n are different names, so print(2 * N) would fail with the same kind of error.
    • The type is fixed. n is an Int, so 2 * n is also an Int, and print shows it as 20.
    • Defining is not running. The lesson's mainFunction contains the call printDouble(10), which is what produces 20. In a standalone Swift script you would write the call on its own line, printDouble(10), below the definition.

    Common mistakes

    • Renaming in only one place. If you change the parameter to num but leave 2 * n in the body, you get the mirror-image error: "cannot find 'n' in scope".
    • Declaring a variable instead. Adding let num = 10 inside the function makes the code compile but prints 20 for every call, because the parameter is ignored. A function with a parameter should use it.
    • Adding a label to the call. Writing printDouble(n: 10) fails, because the underscore removed the label.

    Key takeaways

    • Inside a function, use the parameter's internal name, the one after the underscore.
    • "Cannot find ... in scope" almost always means a typo or a name that was never declared.
    • When a name is flagged, compare it with the header before changing anything else.
    Keep going with SwiftAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free