0
0
Goprogramming~10 mins

Function parameters in Go - Step-by-Step Execution

Choose your learning style9 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.