Go Function Parameters and Arguments

Go Tutorial

 Go Function Parameters and Arguments – Complete Guide with Examples

In Go (Golang), functions can accept inputs that allow them to work with different values. These inputs are called parameters and arguments.

Understanding parameters and arguments is essential for writing reusable, flexible, and clean Go functions.


 What Are Parameters and Arguments?

  • Parameters → Variables defined in the function declaration

  • Arguments → Actual values passed to the function when it is called

Example


 

  •  Parameters receive values
  •  Arguments supply values

 Basic Function Parameters in Go

Syntax

Example

Function Call


 Multiple Parameters

Go allows multiple parameters in a function.

  • Parameters are comma-separated
  • Each parameter has a type

 Short Parameter Type Syntax

When multiple parameters share the same type, you can shorten the syntax.

  •  Cleaner code
  • Common Go style

 Passing Arguments to Functions

Arguments must match:

  • Order

  • Number

  • Type


 

  •  Wrong order or type causes a compile-time error.

 Call by Value in Go (Important)

Go passes arguments by value by default.


 

  •  Original value remains unchanged

 Call by Reference Using Pointers

To modify the original value, use pointers.


 

  • Pass memory address
  • Original value changes

 Passing Arrays, Slices, and Maps

Arrays (Copied)

  •  Arrays are copied

Slices (Reference-like)

  •  Changes reflect outside

Maps (Reference-like)

  •  Original map is modified

 Variadic Parameters (...)

Variadic functions accept any number of arguments.

Usage

  •  Flexible
  •  Common in Go standard library

 Default Parameters (Not Supported)

Go does not support default parameters.

 Invalid

 Alternative


 Named Arguments (Not Supported)

Go does not support named arguments.

 Invalid

  •  Go prefers simplicity and clarity

 Parameter vs Argument – Quick Comparison

FeatureParameterArgument
Defined inFunctionFunction call
Acts asVariableValue
LifetimeInside functionAt call time
TypeRequiredMust match

 Common Mistakes

  •  Passing wrong data type
  •  Expecting call by reference by default
  •  Modifying parameters without pointers
  •  Confusing parameters with arguments
  •  Overusing pointers unnecessarily

 Best Practices

  •  Keep parameter lists short
  •  Use meaningful parameter names
  •  Prefer value passing unless needed
  •  Use pointers for large data
  •  Avoid unnecessary complexity

 Interview Questions: Go Parameters & Arguments

1. What is the difference between parameters and arguments?
Parameters are variables, arguments are values.

2. Does Go support call by reference?
Indirectly, using pointers.

3. Can Go functions have default parameters?
No.

4. What are variadic parameters?
Parameters that accept multiple values.

5. How are slices passed in Go?
By reference-like behavior.


 Summary

  •  Parameters define input
  •  Arguments pass values
  •  Go uses call by value
  •  Pointers enable modification
  •  Variadic functions add flexibility

Mastering function parameters and arguments in Go helps you write modular, reusable, and efficient programs

You may also like...