0
0
Goprogramming~10 mins

Function declaration in Go - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Function declaration
Start
Write func keyword
Name the function
Add parentheses ()
Add parameters (optional)
Add return type (optional)
Write function body { }
Function declared
Call function to use it
This flow shows how to write a function in Go: start with 'func', name it, add parameters and return type if needed, then write the body inside braces.
Execution Sample
Go
func greet(name string) string {
    return "Hello, " + name
}
This code declares a function named greet that takes a name and returns a greeting string.
Execution Table
StepActionCode PartResult
1Start function declarationfunc greet(name string) string {Function named greet starts, takes one string parameter, returns string
2Return greeting stringreturn "Hello, " + nameReturns concatenated string 'Hello, ' + name
3End function body}Function declaration ends
4Call function with argument 'Alice'greet("Alice")Returns 'Hello, Alice'
5Call function with argument 'Bob'greet("Bob")Returns 'Hello, Bob'
💡 Function declaration ends after closing brace; calls return greeting strings.
Variable Tracker
VariableStartAfter greet("Alice")After greet("Bob")
nameundefined"Alice""Bob"
return valueundefined"Hello, Alice""Hello, Bob"
Key Moments - 3 Insights
Why do we write parentheses () even if there are no parameters?
Parentheses show this is a function. Even if empty, they are required syntax to declare or call a function, as seen in step 1 of execution_table.
What does the return type after parentheses mean?
It tells what type the function will give back. Here, 'string' means greet returns a text, shown in step 1 and step 2 of execution_table.
Why do we use curly braces { } after the function declaration?
Curly braces contain the code the function runs. The function body starts after '{' and ends at '}', as shown in steps 1 and 3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the return value when greet is called with "Alice"?
A"Alice"
B"Hello, Bob"
C"Hello, Alice"
DNo return value
💡 Hint
Check step 4 in the execution_table where greet("Alice") is called.
At which step does the function declaration end?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
Look for the closing brace '}' in the execution_table.
If we remove the return type 'string' from the function declaration, what happens?
ACode will not compile
BFunction returns nothing (void)
CFunction returns an int by default
DFunction returns a string anyway
💡 Hint
In Go, the return type is required if the function returns a value; see step 1 in execution_table.
Concept Snapshot
func functionName(parameters) returnType {
    // code to run
}

- 'func' starts the declaration
- Parameters go inside ()
- Return type after () if function returns a value
- Body inside { } with code
- Call function by name with arguments
Full Transcript
This visual shows how to declare a function in Go. First, write 'func' then the function name. Next, add parentheses with parameters if needed. After parentheses, add the return type if the function returns a value. Then write the function body inside curly braces. The example function greet takes a string name and returns a greeting string. The execution table shows each step: starting declaration, returning a value, ending declaration, and calling the function with different names. Variables track the input name and the returned greeting. Key moments explain why parentheses and return types are needed and why curly braces are used. The quiz tests understanding of return values, function end, and syntax rules. This helps beginners see how a Go function is built and used step-by-step.