0
0
Goprogramming~15 mins

Function parameters in Go - Mini Project: Build & Apply

Choose your learning style9 modes available
Function parameters
📖 Scenario: You are creating a simple calculator program in Go. The calculator will add two numbers provided by the user.
🎯 Goal: Build a Go program that defines a function with parameters to add two numbers and then prints the result.
📋 What You'll Learn
Create a function named add that takes two int parameters named a and b.
The add function should return the sum of a and b.
Call the add function with two integer arguments.
Print the result of the add function call.
💡 Why This Matters
🌍 Real World
Functions with parameters let you reuse code for different inputs, like calculators or data processors.
💼 Career
Understanding function parameters is essential for writing clean, reusable code in any software development job.
Progress0 / 4 steps
1
Create the main function
Write the main function with an empty body.
Go
Hint

The main function is the starting point of a Go program.

2
Define the add function with parameters
Define a function named add that takes two int parameters named a and b, and returns an int.
Go
Hint

Use func add(a int, b int) int to define the function with parameters and return type.

3
Call the add function with arguments
Inside the main function, call the add function with arguments 5 and 3, and store the result in a variable named sum.
Go
Hint

Use sum := add(5, 3) to call the function and save the result.

4
Print the result
Add a fmt.Println statement inside main to print the value of sum. Remember to import the fmt package.
Go
Hint

Use fmt.Println(sum) to print the sum. Don't forget to import fmt.