Function parameters let you send information into a function so it can use that information to do its job.
Function parameters in Go
func functionName(paramName paramType) returnType {
// function body
}Parameters are listed inside parentheses after the function name.
You can have multiple parameters separated by commas.
name of type string.func greet(name string) {
fmt.Println("Hello, " + name)
}int parameters and returns their sum.func add(a int, b int) int { return a + b }
func multiply(x, y int) int { return x * y }
This program defines a function greet that takes a name and prints a greeting. In main, it calls greet twice with different names.
package main import "fmt" func greet(name string) { fmt.Println("Hello, " + name + "!") } func main() { greet("Alice") greet("Bob") }
Parameter names should be clear to show what data the function expects.
Parameters are local to the function and cannot be used outside it.
You can have functions with no parameters if they don't need extra information.
Function parameters let you send data into functions.
You list parameters inside parentheses after the function name.
Parameters make functions flexible and reusable with different inputs.
Practice
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 DQuick Check:
Function parameters = send data into functions [OK]
- Thinking parameters create functions
- Confusing parameters with output
- Believing parameters stop function execution
Solution
Step 1: Review Go function parameter syntax
In Go, you can group parameters of the same type likea, 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 forb.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 AQuick Check:
Group same type params: a, b int [OK]
- Writing types before variable names (like C)
- Missing commas between parameters (like D)
- Forgetting type for second parameter (like B)
package main
import "fmt"
func greet(name string) {
fmt.Println("Hello, " + name)
}
func main() {
greet("Alice")
}Solution
Step 1: Understand function call with parameter
The functiongreettakes a string parameternameand prints "Hello, " plus that name.Step 2: Check the argument passed
Inmain,greet("Alice")is called, sonameis "Alice".Final Answer:
Hello, Alice -> Option BQuick Check:
Function prints "Hello, " + argument [OK]
- Confusing parameter name with string literal
- Expecting no output
- Thinking it causes error without return
func multiply(a int, b int) int {
return a * b
}
func main() {
result := multiply(3)
fmt.Println(result)
}Solution
Step 1: Check function call arguments
The functionmultiplyrequires two int parameters, but it is called with only one argument3.Step 2: Identify the error
This causes a compile-time error due to wrong number of arguments.Final Answer:
Function multiply called with wrong number of arguments -> Option AQuick Check:
Function call args must match parameters [OK]
- Ignoring missing import for fmt (not shown here)
- Thinking return type is missing
- Assuming variable declaration error
func sumAndProduct(a, b int) (int, int) {
return a + b, a * b
}
func main() {
s, p := sumAndProduct(4, 5)
fmt.Println(s, p)
}
What will be the output when the program runs?Solution
Step 1: Understand multiple return values
The functionsumAndProductreturns two integers: sum and product ofaandb.Step 2: Check values for inputs 4 and 5
Sum is 4 + 5 = 9, product is 4 * 5 = 20. These are assigned tosandp.Step 3: Print output
fmt.Println(s, p)prints "9 20".Final Answer:
9 20 -> Option CQuick Check:
Multiple returns print sum then product [OK]
- Swapping sum and product values
- Expecting single return value
- Thinking missing return error
Ready to prove it?
Pass the test and this pattern moves unknown
