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, notstring 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
nameand the second intolanguage. Swapping them compiles fine when both are strings, but it printsMy name is Python. - The count and types are checked at compile time. Calling
introduceYourself("John")fails withnot enough arguments. Passing a number where a string is expected fails with a message such ascannot 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
nameinside 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.
- Define the function below the
// Function goes here:comment, withfunc, the nameintroduceYourselfand twostringparameters,nameandlanguage. - Print the name line. Join
"My name is "andnamewith+. - Print the language line the same way, with
"I code in "andlanguage. - Introduce John by calling the function with
"John"and"Python", in that order. - 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 becomesMy 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.