Declaring a Function
Part of the Fundamentals section of Coddy's GO journey — lesson 52 of 109.
Let's learn how to declare a function in Go that takes multiple parameters of the same type.
First, let's create a function that adds three integers together:
func addThree(x, y, z int) int {
return x + y + z
}
func main() {
sum := addThree(5, 10, 15)
fmt.Println("The sum is:", sum)
}After executing this code, the value 30 (which is 5 + 10 + 15) will be calculated and stored in the sum variable. Then the message "The sum is: 30" will show on the screen:
The sum is: 30Notice that we wrote x, y, z int instead of writing x int, y int, z int. This is a helpful shorthand in Go when multiple parameters share the same type - it makes our code shorter and cleaner.
Challenge
BeginnerIn this challenge, you'll practice declaring a function in Go. Your task is to complete the greet function that takes a name as a parameter and returns a greeting message.
The function should return a string in the format: "Hello, [name]!"
Cheat sheet
When multiple parameters share the same type in Go, you can use shorthand syntax:
func addThree(x, y, z int) int {
return x + y + z
}This is equivalent to writing x int, y int, z int but more concise.
Try it yourself
package main
import "fmt"
// TODO: Complete the greet function that takes a name parameter
// and returns a greeting string
// func greet() {
//
// }
func main() {
// These test cases are already set up for you
name1 := "Alice"
name2 := "Bob"
// Testing the greet function
message1 := greet(name1)
message2 := greet(name2)
fmt.Println(message1)
fmt.Println(message2)
}This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Comparison & Logical Operators
Comparison Operators - Part 1Comparison Operators - Part 2Logical AND OperatorLogical OR OperatorLogical NOT OperatorOperator Precedence BasicsRecap - Making Comparisons7Control Flow: Loops
What The `for` Loop ExplainedFor Loop - BasicFor Loop - Condition OnlyThe `break` KeywordThe `continue` KeywordNested LoopsRecap - Repeating Actions2Variables and Basic Data Types
What is a variableType Inference with `:=`Integers (int)Floating-Point NumbersBooleansStringsZero ValuesConstantsNaming ConventionsRecap - Variables and Types5Basic Input/Output
Formatted OutputFormat VerbsPrinting TypesGetting Basic User InputRecap - Input and Output8Functions
Understanding FunctionsDeclaring a FunctionCalling FunctionsFunction ParametersReturning a Single ValueReturning Multiple ValuesNamed Return ValuesFunction Scope BasicsRecap - Creating Reusable Code3Basic Operators
Arithmetic OperatorsDivision OperatorThe Modulo OperatorAssignment OperatorAugmented Assignment OperatorsIncrement and DecrementRecap - Calculations6Control Flow: Conditionals
The `if` StatementThe `else` KeywordThe `else if` KeywordVariable Shadowing in `if`Initializing VariablesThe `switch` StatementSwitch with ExpressionsSwitch without ExpressionThe `fallthrough` KeywordRecap - Making Decisions