Start learning free

LESSON 36 OF 338 · FUNCTIONS I

Using the Right Parameter Name in Go: Fixing the Buggy Code

Practice Read the full explanation

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

When the code is fixed, it should print:

20

But the code has a variable-name mistake. In Go, a function parameter can only be used by the name it was declared with.

For example, if the parameter is named n, the function body should use n, not a different name like num.

Assignment:

Your task is to fix the code so no errors are produced and it prints the desired output.


STEP 1 OF 1
    Ctrl + Enter to check

    GO · FUNCTIONS I

    Buggy Code Non Existing Variable in Go, explained

    What the code is supposed to do

    The function printDouble takes a number and prints twice that number. The call printDouble(10) should print:

    20
    

    The code as given does not compile:

    func printDouble(n int) {
        fmt.Println(2 * num)
    }
    func mainFunction() {
        printDouble(10)
    }
    

    The compiler stops with an error that ends in undefined: num.

    What goes wrong

    The parameter is called n. The body, however, multiplies by num, a name that was never declared. Go has no variable called num anywhere: not in the function, not in the file. When a name can't be found, the compiler reports undefined and names the identifier it could not resolve. Here it is num.

    A function body can use only three kinds of names: its own parameters, the variables it declares itself, and package-level names such as other functions. The call printDouble(10) puts the value 10 into n. It does not create a num, whatever the argument is meant to represent.

    How the value travels

    It helps to follow the 10 through the program. In mainFunction, the call printDouble(10) hands the argument 10 to the function. At the start of the call Go creates the parameter n and stores 10 in it. The body then evaluates 2 * n, which is 20, and passes the result to Println.

    The only link between the call and the body is the parameter's name. The caller can pass a literal, a variable with any name or a more complex expression, and the body always sees it under the name written in the header. This is why the body must use n even when the caller thinks of the value as "the number".

    Fixing it step by step

    1. Read the error. It names num as the undefined identifier and gives the line, which is the fmt.Println line.
    2. Compare with the header. The header declares n int. There is no num.
    3. Make the two names match. Either use n in the body, or rename the parameter to num. The first is the smaller change.
    4. Check the call. printDouble(10) passes one int, which fits the single int parameter, so mainFunction needs no edit.

    The fixed code in Go

    func printDouble(n int) {
        fmt.Println(2 * n)
    }
    func mainFunction() {
        printDouble(10)
    }
    /* Explanation:
    We got an error because inside the original function we used a variable "num", which is not the parameter "n", so it wasn't recognized.
    One fix is to use "n" instead of "num" in the line "fmt.Println(2 * n)"
    Another possible fix is to change the parameter name to "num", like this:
    func printDouble(num int) {
        fmt.Println(2 * num)
    }
    */
    

    Both fixes print 20. Some Go details:

    • The type stays. 2 * n works because n is an int and the constant 2 takes that type.
    • Names are case-sensitive. N and n are different identifiers, so 2 * N would be undefined too.
    • Parameters live inside their function. A name declared in printDouble can't be seen from mainFunction, and the reverse holds as well.
    • Printing numbers needs no conversion. Println accepts values of any type and prints an int as its digits.

    Common mistakes

    • Renaming in only one place. If you rename the parameter to num but leave n somewhere in the body, the error just moves to undefined: n.
    • Declaring num inside the function. Adding a line such as num := 10 would silence the error but print the same number no matter what the caller passes.
    • Changing the call. The call is correct; edits there can't fix an undefined name in the body.
    • Hardcoding 20. fmt.Println(20) passes this call but ignores the parameter entirely.

    Checking your fix

    Run the code in the editor above. The program should compile and print 20. If a new error appears, read its identifier again: it names the exact spelling the compiler could not find.

    Keep going with GoAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free