Menu
Coddy logo textTech

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: 30

Parameters 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 icon

Challenge

Beginner

In 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)
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals