Recall & Review
beginner
What is a return value in Go functions?
A return value is the output that a function sends back to the place where it was called. It lets the function give back a result after doing its work.
Click to reveal answer
beginner
How do you declare a function in Go that returns an integer?
You write the function with the return type after the parameter list. For example:
func add(a int, b int) int { return a + b } This function returns an int.Click to reveal answer
intermediate
Can a Go function return multiple values? Give an example.
Yes, Go functions can return multiple values. For example:
func divide(a, b int) (int, int) { return a / b, a % b } This returns both quotient and remainder.Click to reveal answer
intermediate
What happens if a Go function does not have a return statement but declares a return type?
The Go compiler will give an error because the function promises to return a value but does not. Every path must return a value if a return type is declared.
Click to reveal answer
advanced
Explain named return values in Go functions.
Named return values let you name the results in the function signature. Inside the function, you can set these names like variables and use a bare return to send them back. Example:<pre>func sumAndDiff(a, b int) (sum int, diff int) { sum = a + b; diff = a - b; return }</pre>Click to reveal answer
What does this Go function return?
func greet() string { return "Hello" }✗ Incorrect
The function returns a string "Hello" as declared by the return type string.
How many values does this function return?
func values() (int, string) { return 5, "Go" }✗ Incorrect
The function returns two values: an int and a string.
What will happen if a function declares a return type but has no return statement?
✗ Incorrect
Go requires all functions with return types to have return statements; otherwise, it causes a compile-time error.
Which keyword is used to send back values from a Go function?
✗ Incorrect
The keyword 'return' is used to send back values from a function.
What is special about named return values in Go?
✗ Incorrect
Named return values let you set variables inside the function and use a bare return without specifying values explicitly.
Explain how return values work in Go functions and how to declare them.
Think about how functions send back results after running.
You got /3 concepts.
Describe named return values in Go and how they simplify returning multiple results.
It's like giving names to the boxes you will send back.
You got /3 concepts.