Start learning free

LESSON 38 OF 338 · FUNCTIONS I

Why Parameters and Arguments in Go

Lesson Read the full explanation

Now that we've learned about function parameters and arguments, let's see why they are useful.

Without parameters, every version of an introduction needs its own repeated print statements:

fmt.Println("My name is John")
fmt.Println("I code in Python")

fmt.Println("My name is Mary")
fmt.Println("I code in JavaScript")

That works, but the repeated structure is obvious. A function with parameters lets one function handle different names and languages:

func introduceYourself(name string, language string) {
    fmt.Println("My name is " + name)
    fmt.Println("I code in " + language)
}

func mainFunction() {
    introduceYourself("John", "Python")
    introduceYourself("Mary", "JavaScript")
}

Changing the story:

Need to change the names or languages? Change the arguments in the function calls.

Need to change the introduction text? Change it once inside the function body.


Assignment
Follow the Coding Tutorial and let's write a function with multiple parameters.


Hint
Use the parameters inside the function body just like local variables.


STEP 1 OF 5
    Ctrl + Enter to check

    GO · FUNCTIONS I

    Why Parameters And Arguments in Go, explained

    The problem with repeated code

    Imagine printing a short introduction for two people. Without functions you write every line by hand:

    fmt.Println("My name is John")
    fmt.Println("I code in Python")
    
    fmt.Println("My name is Mary")
    fmt.Println("I code in JavaScript")
    

    It works, but the two blocks have the same shape. Only the name and the language differ. With ten people you would write twenty lines, and changing the wording of the introduction would mean editing all twenty.

    Parameters make one function do the work

    A parameter is a variable listed in the function's header. It is a placeholder for a value the caller will provide. An argument is the actual value passed in a call. In Go, a parameter is written as its name followed by its type:

    func introduceYourself(name string, language string) {
        fmt.Println("My name is " + name)
        fmt.Println("I code in " + language)
    }
    

    This header declares two parameters, name and language, both of type string. Inside the body they behave like local variables that already hold a value. Now each introduction is one call:

    introduceYourself("John", "Python")
    introduceYourself("Mary", "JavaScript")
    

    The result of the two calls is:

    My name is John
    I code in Python
    My name is Mary
    I code in JavaScript
    

    This gives you two places to make changes. To change who is introduced, change the arguments in the calls. To change the wording, change the body once, and every call follows.

    Go details about parameters

    • The type comes after the name. Write name string, not string name.
    • Neighbouring parameters can share a type. func introduceYourself(name, language string) means exactly the same as the longer header above.
    • Arguments match parameters by position. The first argument goes into name and the second into language. Swapping them compiles fine when both are strings, but it prints My name is Python.
    • The count and types are checked at compile time. Calling introduceYourself("John") fails with not enough arguments. Passing a number where a string is expected fails with a message such as cannot use 5 (untyped int constant) as string value. There are no optional parameters and no default values in Go.
    • Arguments are copied. The function receives its own copy of each value, so assigning to name inside the function doesn't change anything in the caller.
    • Strings are joined with +. Go doesn't insert anything between the parts, so the trailing space in "My name is " is needed.

    Why not one long list of prints?

    A function also gives the repeated logic a name. introduceYourself("John", "Python") reads like a sentence, while four fmt.Println lines make the reader work out what they do together. As the program grows, you will keep wanting to say what should happen once, in one place, and let the calls decide the details. Parameters are what make that possible.

    The exercise, step by step

    The lesson asks for the function introduceYourself and two calls inside mainFunction.

    1. Define the function below the // Function goes here: comment, with func, the name introduceYourself and two string parameters, name and language.
    2. Print the name line. Join "My name is " and name with +.
    3. Print the language line the same way, with "I code in " and language.
    4. Introduce John by calling the function with "John" and "Python", in that order.
    5. Introduce Mary with a second call, using "Mary" and "JavaScript". Only the arguments change.
    // Function goes here:
    func introduceYourself(name string, language string) {
        fmt.Println("My name is " + name)
        fmt.Println("I code in " + language)
    }
    
    
    func mainFunction() {
        // Introduce John:
        introduceYourself("John", "Python")
    
        // Introduce Mary:
        introduceYourself("Mary", "JavaScript")
    
    }
    

    Common mistakes

    • Leaving out the space in "My name is ". The output becomes My name isJohn.
    • Putting the parameter name in quotes. "name" is the word, not the value.
    • Passing the arguments in the wrong order. With two string parameters the compiler can't notice, so check the output.
    • Putting the calls outside mainFunction. In this lesson the calls belong inside it, under the two comments.

    Key takeaways

    • Parameters are placeholders in the header, and arguments are the values you pass in a call.
    • In Go the type follows the parameter name, and arguments are matched by position.
    • One function with parameters replaces many near-identical blocks, and a change to the logic is made in one place.
    Keep going with GoAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free