0
0
Goprogramming~15 mins

Return values in Go - Mini Project: Build & Apply

Choose your learning style9 modes available
Return values
📖 Scenario: You are creating a simple calculator program in Go. You want to write a function that adds two numbers and returns the result.
🎯 Goal: Build a Go program with a function that takes two numbers, adds them, and returns the sum. Then print the returned result.
📋 What You'll Learn
Create a function called add that takes two int parameters and returns an int
Call the add function with two numbers
Store the returned value in a variable
Print the stored result
💡 Why This Matters
🌍 Real World
Functions that return values are used everywhere in programming to get results from calculations or data processing.
💼 Career
Understanding return values is essential for writing reusable code and building programs that perform tasks and give back results.
Progress0 / 4 steps
1
Create the add function
Write a function called add that takes two int parameters named a and b and returns their sum as an int. The function should return a + b.
Go
Hint

Use the return keyword to send back the sum of a and b.

2
Call the add function and store the result
Inside the main function, call the add function with the numbers 5 and 7. Store the returned value in a variable called result.
Go
Hint

Use := to declare and assign result in one step.

3
Print the result
Add a fmt.Println statement inside main to print the value stored in result. Remember to import the fmt package at the top.
Go
Hint

Use fmt.Println(result) to show the sum on the screen.

4
Run the program to see the output
Run the program. It should print the sum of 5 and 7, which is 12.
Go
Hint

Check the terminal or console output. It should show 12.