Bird
Raised Fist0
Goprogramming~10 mins

Function parameters in Go - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Function parameters
Define function with parameters
↓
Call function with arguments
↓
Arguments passed to parameters
↓
Function body uses parameters
↓
Function returns result or ends
This flow shows how a function is defined with parameters, called with arguments, and how those arguments are passed to parameters for use inside the function.
Execution Sample
Go
package main
import "fmt"
func greet(name string) {
    fmt.Println("Hello,", name)
}
func main() {
    greet("Alice")
}
This code defines a function greet with a parameter name and calls it with the argument "Alice" to print a greeting.
Execution Table
StepActionParameter 'name' ValueOutput
1Call greet("Alice")Alice
2Enter greet functionAlice
3Execute fmt.Println("Hello,", name)AliceHello, Alice
4End greet functionAlice
5Return to main
💡 Function greet finishes execution and returns control to main.
Variable Tracker
VariableStartAfter CallAfter PrintFinal
nameundefinedAliceAliceundefined
Key Moments - 2 Insights
Why does the parameter 'name' have the value 'Alice' inside the function?
Because when greet is called with the argument "Alice" (see execution_table step 1), the value "Alice" is passed to the parameter 'name'.
What happens if we call greet with a different argument?
The parameter 'name' will take that new argument's value during the function call, changing what is printed (see execution_table step 1).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of parameter 'name' at step 3?
A"Alice"
B"Hello"
Cundefined
D"greet"
💡 Hint
Check the 'Parameter 'name' Value' column at step 3 in the execution_table.
At which step does the function greet print the greeting?
AStep 1
BStep 3
CStep 4
DStep 5
💡 Hint
Look at the 'Output' column in the execution_table to find when printing happens.
If we call greet("Bob") instead of greet("Alice"), what changes in the variable_tracker?
AThe 'name' variable will be 'Alice'
BThe 'name' variable will be undefined
CThe 'name' variable will be 'Bob' after call
DNo change in 'name' variable
💡 Hint
Variable 'name' tracks the argument passed to the function as shown in variable_tracker.
Concept Snapshot
Function parameters in Go:
- Define parameters in function signature: func f(param type)
- Call function with arguments: f(value)
- Arguments passed to parameters by value
- Parameters used inside function body
- Function ends and returns control
Full Transcript
This example shows how a function in Go uses parameters. The function greet has one parameter called name of type string. When greet is called with the argument "Alice", the value "Alice" is passed to the parameter name. Inside the function, the parameter name holds the value "Alice" and is used to print a greeting message. After printing, the function ends and returns control to main. The variable tracker shows how the parameter name changes from undefined before the call to "Alice" during the function execution and then back to undefined after the function ends.

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

  1. Step 1: Understand what parameters do

    Parameters let you give information to a function when you call it.
  2. Step 2: Identify the correct purpose

    Sending data into functions makes them flexible and reusable with different inputs.
  3. Final Answer:

    To send data into functions so they can use it -> Option D
  4. 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

  1. Step 1: Review Go function parameter syntax

    In Go, you can group parameters of the same type like a, b int.
  2. 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.
  3. Final Answer:

    func add(a, b int) int {} -> Option A
  4. Quick Check:

    Group same type params: a, b int [OK]
Hint: Group same type params with commas before type [OK]
Common Mistakes:
  • Writing types before variable names (like C)
  • Missing commas between parameters (like D)
  • Forgetting type for second parameter (like B)
3. What will be the output of this Go program?
package main
import "fmt"
func greet(name string) {
    fmt.Println("Hello, " + name)
}
func main() {
    greet("Alice")
}
medium
A. Hello,
B. Hello, Alice
C. Hello, name
D. Compilation error

Solution

  1. Step 1: Understand function call with parameter

    The function greet takes a string parameter name and prints "Hello, " plus that name.
  2. Step 2: Check the argument passed

    In main, greet("Alice") is called, so name is "Alice".
  3. Final Answer:

    Hello, Alice -> Option B
  4. Quick Check:

    Function prints "Hello, " + argument [OK]
Hint: Function prints greeting plus passed name [OK]
Common Mistakes:
  • Confusing parameter name with string literal
  • Expecting no output
  • Thinking it causes error without return
4. Identify the error in this Go function declaration:
func multiply(a int, b int) int {
    return a * b
}

func main() {
    result := multiply(3)
    fmt.Println(result)
}
medium
A. Function multiply called with wrong number of arguments
B. Missing import for fmt package
C. Return type missing in multiply function
D. Variable result not declared

Solution

  1. Step 1: Check function call arguments

    The function multiply requires two int parameters, but it is called with only one argument 3.
  2. Step 2: Identify the error

    This causes a compile-time error due to wrong number of arguments.
  3. Final Answer:

    Function multiply called with wrong number of arguments -> Option A
  4. Quick Check:

    Function call args must match parameters [OK]
Hint: Count arguments to match parameters exactly [OK]
Common Mistakes:
  • Ignoring missing import for fmt (not shown here)
  • Thinking return type is missing
  • Assuming variable declaration error
5. Given this Go function:
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?
hard
A. 20 9
B. 9
C. 9 20
D. Compilation error due to missing return

Solution

  1. Step 1: Understand multiple return values

    The function sumAndProduct returns two integers: sum and product of a and b.
  2. Step 2: Check values for inputs 4 and 5

    Sum is 4 + 5 = 9, product is 4 * 5 = 20. These are assigned to s and p.
  3. Step 3: Print output

    fmt.Println(s, p) prints "9 20".
  4. Final Answer:

    9 20 -> Option C
  5. Quick Check:

    Multiple returns print sum then product [OK]
Hint: Multiple returns unpacked in order [OK]
Common Mistakes:
  • Swapping sum and product values
  • Expecting single return value
  • Thinking missing return error
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard