func, followed by the function name and a pair of parentheses. Within the parentheses, you define the input parameters. After the parameters, you specify the return type, if any. The function body is enclosed within curly braces. The general function definition appears as follows:
Understanding the function signature helps you quickly know how to call the function and what value to expect in return.
Example: Adding Two Numbers
Consider a simple functionaddNumbers that accepts two integers and returns their sum. Below is how you can implement it in Go:
- The signature is
func addNumbers(a int, b int) int. - The function computes the sum inside its body and uses the
returnstatement to pass that value back to the caller.
Naming Conventions
In Go, function names must adhere to specific naming rules:- They must begin with a letter or an underscore.
- They can include additional letters, digits, and underscores.
- They are case sensitive.
- They cannot contain spaces.

Parameters vs. Arguments
It’s important to distinguish between parameters and arguments:- Parameters are the variable names defined in the function signature.
- Arguments are the actual values passed to the function when it is called.
aandbare parameters.2and3are the arguments provided when invokingaddNumbers.
Input Parameters and Return Values
Go functions handle two types of parameters:- Input Parameters: These values are passed into the function either by value or by address.
- Return Parameters: These define the outputs returned by the function.
printGreeting function takes a single string argument and prints a customized greeting:
printGreeting("Joe") is called, the argument "Joe" is passed to the function, and the message “Hey there, Joe” is printed to the console.
Practice by writing your own functions to reinforce your understanding of input, parameters, and return values.