Help for the VSCode editor.
-
Define a function with following specs, and choose the correct definition
- Name of function:
returnCube
- Input parameters:
int
- Output parameters:
int
- The function calculates the cube of
n
(n*n*n
)
Is it -
-
func returnCube(n) int { return n*n*n }
-
func returnCube(int) int { return n*n*n }
-
func returnCube(n int) int { return n*n*n }
-
funct returnCube(n int) int { return n*n*n }
Reveal
func returnCube(n int) int { return n*n*n }
Function syntax is as follows
- keyword
func
(notfunct
) - input parameters surrounded with brackets
()
, or empty brackets if no input parameters. - Each input parameter is variable name followed by type, e.g.
n int
. - After
)
is the type of what we want to return (if anything), in this caseint
- Lastly
{
to begin the function body.
Then we have the code of the function followed by
}
to close it off - Name of function:
-
What would be the output of the following program: (add package declarations and import statements as needed)
func returnCube(n int) int { return n*n*n } func main() { returnCube(5) }
- no output
- error
- 5 125
- 125
Reveal
no output
Why? Becuase we are not doing anything with the value we get from
returnCube
, certainly not printing it!Note that to try this program, you would have to include
package main
as the first line. -
Select valid function declarations from the below options.
func 3doSomething(n int) int
func doSomething(a int, b int) int
func doSomething(n int) int
func do Something(n int) int
Reveal
B. C
- A is incorrect because an identifier (function name, variable name etc.) cannot start with a digit.
- D is incorrect because an identifier cannot contain whitespace.
-
Choose the correct function declaration of function printDetails so the following code can be executed successfully in main function.
var s string s = printDetails("Joe") fmt.Print(s)
and gives the following output
Joe 1
func printDetails(s string) int
func print_Details(s string) string
func printDetails(s int) string
func printDetails(s string) string
Reveal
func printDetails(s string) string
To answer this, you do not need to be concerned about the actual output other then realizing that it must be a string. What you do need to be concerned about is selecting the correct function signature from the four options.
func printDetails(s string) int
- is incorrect as it returns anint
. Not possible to get the required output with an int return type.func print_Details(s string) string
- is incorrect because the function is named incorrectly.func printDetails(s int) string
- returns the correct type, but is incorrect because its input isint
- can't passJoe
as an int parameter.
-
What would be the output of the following program:
func printDetails(s string) { fmt.Println(s) } func main() { x := printDetails("Joe") fmt.Print(x) }
- error
- empty string
- nil
- 0
Reveal
error
The program will not compile due to this
x := printDetails("Joe")
Look at
printDetails
function. Does it return anything? No, so you can't assign a variable from the result of a call to this function.