Calling Functions
Part of the Fundamentals section of Coddy's GO journey — lesson 53 of 109.
Calling Functions
Let's learn how to call functions in Go. Calling a function means executing a function that has been declared so we can use its functionality.
First, let's create a simple greeting function and call it with different names:
func greet(name string) {
fmt.Println("Hello,", name)
}
func main() {
greet("Alex")
greet("Taylor")
}After executing this code, we'll see two greeting messages on the screen:
Hello, Alex
Hello, TaylorNow, let's call a function that returns a value and store that result in a variable:
func addThree(a, b, c int) int {
return a + b + c
}
func main() {
result := addThree(10, 20, 30)
fmt.Println(result)
}After executing this code, the function will add the three numbers together and return the sum. The variable result will store the value 60, and then we'll see this output on the screen:
60That's how we call functions in Go - simply write the function name followed by parentheses containing any required arguments. If the function returns a value, we can capture it in a variable.
Challenge
BeginnerIn this challenge, you'll practice calling functions in Go. We've defined three simple functions for you: sayHello, sayGoodbye, and sayThankYou. Your task is to call each of these functions in the correct order to produce the expected output.
Each function simply prints a greeting message. You just need to call them in the right sequence.
Cheat sheet
To call a function in Go, write the function name followed by parentheses containing any required arguments:
func greet(name string) {
fmt.Println("Hello,", name)
}
func main() {
greet("Alex")
greet("Taylor")
}To call a function that returns a value and store the result:
func addThree(a, b, c int) int {
return a + b + c
}
func main() {
result := addThree(10, 20, 30)
fmt.Println(result)
}Try it yourself
package main
import "fmt"
// This function prints a hello message
func sayHello() {
fmt.Println("Hello, friend!")
}
// This function prints a goodbye message
func sayGoodbye() {
fmt.Println("Goodbye, friend!")
}
// This function prints a thank you message
func sayThankYou() {
fmt.Println("Thank you, friend!")
}
func main() {
// TODO: Call the sayHello function
// TODO: Call the sayThankYou function
// TODO: Call the sayGoodbye function
}
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