Skip to main content

Parameters and Arguments

To hand information to a function, you give it parameters. Inside the function, each parameter behaves like a variable.

You list them in the parentheses after the function name, each with its type. You can have as many as you need; commas separate them.

Parameter Syntax

main.go
func FunctionName(param1 type, param2 type, param3 type) {
  // code to be executed
}

Function With Parameter Example

In this example, familyName() takes one parameter, fname, of type string. Each call passes in a different first name, which the function uses in its message. So the first name changes from line to line while the last name stays the same.

Note: The value you pass in a call has its own name: an argument. In this example, fname is the parameter, and Liam, Jenny and Anja are the arguments.

Example

Prompt
Create a function familyName that takes a string parameter fname and prints "Hello" followed by fname and "Refsnes". Call it with "Liam", "Jenny", and "Anja".
Output: Go program and captured output

Multiple Parameters

A function is not limited to one parameter; it can take as many as you need. Here familyName() takes a name and an age.

Note: With several parameters, each call has to supply one argument per parameter, in the same order as they are declared.

Example

Prompt
Create a function familyName that takes a string fname and an int age, then prints a greeting with all values. Call it three times with different arguments.
Output: Go program and captured output