Go Function Parameters and Arguments

Functions in Go can accept data in the form of parameters. These parameters act as variables within the function and allow data to be passed and processed. When you invoke a function and provide values, these are referred to as arguments.

Tutorials dojo strip

Parameters and their types are specified within parentheses () following the function name. Multiple parameters should be separated with commas.



Syntax for Parameters

Go




Example 1: Function with a Single Parameter

Go

Output:

Go

Explanation:

  • motorcycleBrand is a function with one parameter, brand.
  • When the function is called (e.g., motorcycleBrand("Ducati")), the argument "Ducati" is passed into the brand parameter.


Difference Between Parameters and Arguments

  • Parameters: Declared in the function definition (e.g., brand in the example above).
  • Arguments: The actual values passed to the function when called (e.g., "Ducati", "Yamaha", "Honda").




Example 2: Function with Multiple Parameters

Functions can accept multiple parameters of different types. This example includes two parameters: brand (a string) and rating (an int).

Go

Output:

Go

Notes on Multiple Parameters

  • The number of arguments in the function call must match the number of parameters in the function definition.
  • Arguments must be passed in the same order as the parameters are declared.
Tutorials dojo strip