📖 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.
Practice
(1/5)
1. What is the purpose of function parameters in Go?
easy
A. To print output from a function
B. To create new functions automatically
C. To stop a function from running
D. To send data into functions so they can use it
Solution
Step 1: Understand what parameters do
Parameters let you give information to a function when you call it.
Step 2: Identify the correct purpose
Sending data into functions makes them flexible and reusable with different inputs.
Final Answer:
To send data into functions so they can use it -> Option D
Quick Check:
Function parameters = send data into functions [OK]
Hint: Parameters pass data into functions for reuse [OK]
Common Mistakes:
Thinking parameters create functions
Confusing parameters with output
Believing parameters stop function execution
2. Which of the following is the correct way to declare a function with two integer parameters in Go?
easy
A. func add(a, b int) int {}
B. func add(a int, b) int {}
C. func add(int a, int b) int {}
D. func add(a int b int) int {}
Solution
Step 1: Review Go function parameter syntax
In Go, you can group parameters of the same type like a, b int.
Step 2: Identify correct syntax
func add(a, b int) int {} groups same type parameters correctly. func add(a int, b) int {} misses type for b. func add(int a, int b) int {} places types before names. func add(a int b int) int {} lacks commas.
Final Answer:
func add(a, b int) int {} -> Option A
Quick Check:
Group same type params: a, b int [OK]
Hint: Group same type params with commas before type [OK]