Function Parameters
Part of the Fundamentals section of Coddy's GO journey — lesson 54 of 109.
Function parameters are values you pass to a function when calling it. They allow functions to work with different data each time they're called.
Create a function with parameters by listing their names and types in the parentheses:
func add(x int, y int) {
sum := x + y
fmt.Println("Sum:", sum)
}
func main() {
add(5, 3)
add(10, 20)
}When you run this program, it outputs:
Sum: 8
Sum: 30Parameters with the same type can be written more concisely:
func greet(first, last string) {
fmt.Println("Hello,", first, last)
}Functions can also return values by specifying a return type:
func multiply(x, y int) int {
return x * y
}
func main() {
result := multiply(4, 5)
fmt.Println("Result:", result) // Output: Result: 20
}Challenge
BeginnerIn this challenge, you'll practice working with function parameters. You need to complete a function that takes two string parameters and combines them to create a greeting message.
The function createGreeting is already defined, but it's missing the parameters. Your task is to add the correct parameters and use them in the function body.
Cheat sheet
Function parameters allow functions to work with different data each time they're called.
Define parameters by listing their names and types in parentheses:
func add(x int, y int) {
sum := x + y
fmt.Println("Sum:", sum)
}Parameters with the same type can be written more concisely:
func greet(first, last string) {
fmt.Println("Hello,", first, last)
}Functions can return values by specifying a return type:
func multiply(x, y int) int {
return x * y
}Try it yourself
package main
import "fmt"
// TODO: Add parameters to this function to accept a greeting and a name
// The function should take two string parameters: greeting and name
// You can write them as (greeting string, name string) or (greeting, name string)
func createGreeting(/* add parameters here */) string {
// TODO: Return a string that combines the greeting and name
// For example: "Hello, John!"
// Use the format: greeting + ", " + name + "!"
}
func main() {
// These test calls are already defined for you
message := createGreeting("Hello", "Gopher")
fmt.Println(message)
// Test with different values
message = createGreeting("Welcome", "Friend")
fmt.Println(message)
}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