0
0
Goprogramming~5 mins

Function declaration in Go - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is a function declaration in Go?
A function declaration in Go defines a reusable block of code with a name, optional parameters, and an optional return type. It tells the program what the function does when called.
Click to reveal answer
beginner
How do you declare a simple function named greet that prints "Hello" in Go?
You write:<br>
func greet() {
    fmt.Println("Hello")
}
<br>This declares a function named greet with no parameters and no return value.
Click to reveal answer
intermediate
What is the syntax to declare a function with parameters in Go?
You list parameters inside parentheses after the function name, each with a name and type. For example:<br>
func add(a int, b int) int {
    return a + b
}
<br>This function takes two integers and returns an integer.
Click to reveal answer
intermediate
Can a Go function return multiple values? How?
Yes, Go functions can return multiple values by listing return types in parentheses. Example:<br>
func swap(x, y string) (string, string) {
    return y, x
}
<br>This function returns two strings.
Click to reveal answer
beginner
What keyword starts a function declaration in Go?
The keyword func starts every function declaration in Go. It signals that a function is being defined.
Click to reveal answer
Which keyword is used to declare a function in Go?
Afunc
Bfunction
Cdef
Dfun
How do you declare a function named sum that takes two integers and returns an integer?
Afunction sum(a, b int) int {}
Bfunc sum(a int, b int) {}
Cfunc sum(a int, b int) int {}
Ddef sum(a int, b int) int {}
Can a Go function return more than one value?
ANo, Go does not support return values
BNo, only one return value is allowed
CYes, but only if parameters are also multiple
DYes, by listing multiple return types in parentheses
What is the correct way to declare a function with no parameters and no return value?
Afunc hello() {}
Bfunc hello {}
Cfunc hello() -> void {}
Dfunction hello() {}
Which of these is a valid function declaration in Go?
Afunction greet(name string) string { return "Hi " + name }
Bfunc greet(name string) string { return "Hi " + name }
Cfunc greet(name string) { return "Hi " + name }
Dfunc greet(name) string { return "Hi " + name }
Explain how to declare a function in Go that takes parameters and returns a value.
Think about the order: func, name, parameters, return type, then body.
You got /5 concepts.
    Describe how Go functions can return multiple values and why this might be useful.
    Remember Go often returns error as a second value.
    You got /3 concepts.