Bird
Raised Fist0
Goprogramming~5 mins

Return values in Go - Cheat Sheet & Quick Revision

Choose your learning style10 modes available
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" }
AA string
BAn integer
CNothing
DA boolean
How many values does this function return?
func values() (int, string) { return 5, "Go" }
AOne
BZero
CThree
DTwo
What will happen if a function declares a return type but has no return statement?
AIt compiles fine
BIt causes a compile-time error
CIt returns zero by default
DIt returns nil
Which keyword is used to send back values from a Go function?
Areturn
Bsend
Cyield
Doutput
What is special about named return values in Go?
AThey are deprecated
BThey require explicit return values every time
CThey allow returning values without specifying them in the return statement
DThey are only used for error handling
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.

      Practice

      (1/5)
      1. What does a function's return value in Go do?
      easy
      A. It defines the function's name.
      B. It comments the code for documentation.
      C. It declares variables inside the function.
      D. It sends a result back to the place where the function was called.

      Solution

      1. Step 1: Understand the purpose of return values

        Return values are used to send results from a function back to the caller.
      2. Step 2: Identify the correct description

        Only It sends a result back to the place where the function was called. correctly describes this purpose.
      3. Final Answer:

        It sends a result back to the place where the function was called. -> Option D
      4. Quick Check:

        Return value = sends result back [OK]
      Hint: Return values send results back to the caller [OK]
      Common Mistakes:
      • Confusing return values with variable declarations
      • Thinking return values name the function
      • Assuming return values are comments
      2. Which of the following is the correct way to declare a function in Go that returns an integer?
      easy
      A. func add() int { return 5 }
      B. func add() { return 5 }
      C. func add int() { return 5 }
      D. func add() int return 5

      Solution

      1. Step 1: Check function syntax for return type

        In Go, the return type is specified after parentheses: func name() type { ... }
      2. Step 2: Verify each option

        func add() int { return 5 } correctly declares a function returning int. Others have syntax errors.
      3. Final Answer:

        func add() int { return 5 } -> Option A
      4. Quick Check:

        Return type after () = int [OK]
      Hint: Return type goes after parentheses in function declaration [OK]
      Common Mistakes:
      • Placing return type before function name
      • Omitting return type when returning a value
      • Incorrect function syntax
      3. What is the output of this Go code?
      func multiply(a, b int) int {
          return a * b
      }
      
      func main() {
          result := multiply(3, 4)
          fmt.Println(result)
      }
      medium
      A. 7
      B. 34
      C. Error: missing import
      D. 12

      Solution

      1. Step 1: Check for required imports

        The code uses fmt.Println(result) but lacks import "fmt", causing a compile error: undefined fmt.
      2. Step 2: Verify function logic

        The multiply function correctly computes 3*4=12, but the program won't compile or run without the import.
      3. Final Answer:

        Error: missing import -> Option C
      4. Quick Check:

        fmt.Println needs import "fmt" [OK]
      Hint: fmt.Println requires import "fmt" [OK]
      Common Mistakes:
      • Forgetting import "fmt"
      • Computing 3*4=12 but ignoring missing import
      • Assuming code runs without imports
      4. Identify the error in this Go function that returns two integers:
      func divide(a, b int) (int, int) {
          return a / b
      }
      medium
      A. Division operator is incorrect.
      B. Function returns only one value but declares two return types.
      C. Function parameters are missing types.
      D. Function name is invalid.

      Solution

      1. Step 1: Check return type declaration

        The function declares two return values (int, int) but returns only one value.
      2. Step 2: Identify mismatch in return statement

        Returning a single value causes a compile error because two values are expected.
      3. Final Answer:

        Function returns only one value but declares two return types. -> Option B
      4. Quick Check:

        Return count must match declared types [OK]
      Hint: Return values count must match declared types [OK]
      Common Mistakes:
      • Returning fewer values than declared
      • Wrong operator usage confusion
      • Ignoring function signature
      5. How can you write a Go function that returns both the sum and product of two integers?
      hard
      A. func calc(a, b int) (int, int) { return a+b, a*b }
      B. func calc(a, b int) int { return a+b, a*b }
      C. func calc(a, b int) (int) { return a+b, a*b }
      D. func calc(a, b int) (int, int) { return a+b + a*b }

      Solution

      1. Step 1: Define function with two return types

        The function must declare two return types (int, int) for sum and product.
      2. Step 2: Return sum and product as two values

        Return both values separated by a comma: return a+b, a*b.
      3. Final Answer:

        func calc(a, b int) (int, int) { return a+b, a*b } -> Option A
      4. Quick Check:

        Two return values with matching types [OK]
      Hint: Return multiple values separated by commas [OK]
      Common Mistakes:
      • Returning multiple values with single return type
      • Returning combined expression instead of two values
      • Incorrect return type syntax
      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