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
- Read the error. It names the identifier the compiler couldn't find:
num. - Find where it is used. It appears in the
printline insideprintDouble. - Compare it with the header. The parameter is called
n, notnum. - Make the two agree. The simplest fix is to use
nin the body. The other fix is to rename the parameter to_ num: Intand 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.
Nandnare different names, soprint(2 * N)would fail with the same kind of error. - The type is fixed.
nis anInt, so2 * nis also anInt, andprintshows it as20. - Defining is not running. The lesson's
mainFunctioncontains the callprintDouble(10), which is what produces20. 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
numbut leave2 * nin the body, you get the mirror-image error: "cannot find 'n' in scope". - Declaring a variable instead. Adding
let num = 10inside the function makes the code compile but prints20for 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.
