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.
Free plan: 30 tutor messages. PRO is unlimited.
