What happens first when a function is called in Go?
The program jumps to the function's code and starts executing from the first line inside the function.
Click to reveal answer
beginner
What is the role of the return statement in a function?
It ends the function execution and optionally sends a value back to where the function was called.
Click to reveal answer
intermediate
How does Go handle multiple return values in a function?
Go functions can return multiple values by listing them in the function signature and the return statement.
Click to reveal answer
beginner
What happens if a function does not have a return statement?
If the function's return type is void (no return), it simply finishes execution and control goes back to the caller.
Click to reveal answer
intermediate
Explain the flow when a function calls another function inside it.
The first function pauses, the called function runs completely, then control returns to the first function to continue.
Click to reveal answer
What does the Go program do immediately after a function finishes executing?
ATerminates the program
BStarts executing the main function again
CReturns control to the point where the function was called
DSkips to the next function in the file
✗ Incorrect
After a function finishes, the program returns to the exact place where the function was called to continue execution.
Which keyword is used to send a value back from a function in Go?
Areturn
Bsend
Coutput
Dyield
✗ Incorrect
The 'return' keyword ends the function and sends back the specified value(s).
If a function calls another function, what happens to the first function?
AIt continues running in parallel
BIt pauses until the called function finishes
CIt ends immediately
DIt restarts from the beginning
✗ Incorrect
The first function waits (pauses) until the called function completes before continuing.
Can a Go function return more than one value?
ANo, only one value is allowed
BOnly if it returns a slice
COnly if it uses pointers
DYes, by listing multiple return types
✗ Incorrect
Go supports multiple return values by specifying them in the function signature and return statement.
What happens if a function with no return type reaches the end of its code?
AIt ends and returns control to the caller
BIt causes a compile error
CIt waits for a return statement
DIt restarts automatically
✗ Incorrect
Functions without return types simply finish execution and return control to the caller.
Describe the step-by-step flow when a Go function is called and then returns a value.
Think about what happens from the moment you call the function until you get the result.
You got /5 concepts.
Explain how Go handles a function that calls another function inside it.
Imagine you ask a friend to do something before you continue your task.
You got /4 concepts.
Practice
(1/5)
1. What happens when a function in Go is called?
easy
A. The code inside the function runs from top to bottom.
B. The program stops and waits for user input.
C. The function immediately returns without running any code.
D. The function runs only if it has no parameters.
Solution
Step 1: Understand function call behavior
When a function is called, Go executes the code inside it in order, from the first line to the last.
Step 2: Eliminate incorrect options
Options A, B, and C describe behaviors that are not how Go functions work when called.
Final Answer:
The code inside the function runs from top to bottom. -> Option A
Quick Check:
Function execution flow = top to bottom [OK]
Hint: Functions run code line by line when called [OK]
Common Mistakes:
Thinking functions run only if no parameters
Believing functions wait for user input automatically
Assuming functions return immediately without running code
2. Which of the following is the correct way to define a function named greet that returns a string in Go?
easy
A. func greet() { return "Hello" }
B. function greet() { return "Hello" }
C. func greet() string { return "Hello" }
D. func greet() string { print("Hello") }
Solution
Step 1: Check Go function syntax
In Go, functions start with func, followed by the name, parameters, and return type. func greet() string { return "Hello" } matches this.
Step 2: Verify return statement and type
func greet() string { return "Hello" } returns a string and has the correct syntax. The version using 'function greet() { return "Hello" }' uses wrong keyword. The version 'func greet() { return "Hello" }' misses return type. The version using print instead of return is incorrect.
Final Answer:
func greet() string { return "Hello" } -> Option C
Quick Check:
Correct Go function syntax = func greet() string { return "Hello" } [OK]
Hint: Go functions start with func and specify return type [OK]
Common Mistakes:
Using 'function' keyword instead of 'func'
Omitting return type when function returns a value
Using print instead of return for output
3. What is the output of this Go program?
package main
import "fmt"
func add(a int, b int) int {
return a + b
}
func main() {
result := add(3, 4)
fmt.Println(result)
}
medium
A. 34
B. Compilation error
C. 0
D. 7
Solution
Step 1: Understand function call and return
The function add takes two integers and returns their sum. Calling add(3, 4) returns 7.
Step 2: Check output statement
fmt.Println(result) prints the returned value, which is 7.
Final Answer:
7 -> Option D
Quick Check:
3 + 4 = 7 [OK]
Hint: Add numbers inside function, print returned result [OK]
Common Mistakes:
Concatenating numbers as strings (34)
Expecting zero without return
Thinking code has syntax errors
4. Identify the error in this Go function and select the fix:
func multiply(a int, b int) int {
product := a * b
}
func main() {
fmt.Println(multiply(2, 3))
}
medium
A. Change function to void (no return type)
B. Add a return statement: return product
C. Remove parameters a and b
D. Replace * with + operator
Solution
Step 1: Check function return type and body
The function declares it returns an int but does not have a return statement, causing a compile error.
Step 2: Fix by adding return statement
Adding return product returns the calculated value, matching the function signature.
Final Answer:
Add a return statement: return product -> Option B
Quick Check:
Function must return value if declared [OK]
Hint: Functions with return type must return a value [OK]
Common Mistakes:
Forgetting return statement in non-void functions
Changing function signature unnecessarily
Changing operators without reason
5. Given this Go code, what will be printed?
package main
import "fmt"
func process(x int) int {
if x > 0 {
return x * 2
}
return 0
}
func main() {
values := []int{3, -1, 0, 5}
sum := 0
for _, v := range values {
sum += process(v)
}
fmt.Println(sum)
}
hard
A. 16
B. 8
C. 0
D. Compilation error
Solution
Step 1: Analyze function process behavior
For each value, if positive, returns double the value; else returns 0.