project: Assessment: Functions, return statements and printing to the terminal

Tags technical-assessment skill/return_and_print

Students should 100% understand the following concepts. Note that simply memorizing this stuff will be insufficient. In the final test, the different concepts will be combined in complex ways.

Make use of print statements to check that the student knows what order things will happen in.

Note about submission format

On Tilde you will notice that this card is asking for a link submission. Please don’t worry about submitting a link. You will be assessed according to TOPIC: Introduction to live assessments

Return and print are different things

A lot of people get this wrong because they learn to code using a REPL. Java folks are less likely to fall into this trap.

Here are some basic code examples. It is in Python but easy to translate to other languages.

def foo():
    print("hi")

x = foo()
print(f"x = {x}")

Many think that the code will print x = hi. It does not.

Multiple different return statements inside a single function

For example:

def foo(colour):
    if colour == "blue":
        return 1
    return 2

Multiple function calls

x = foo("blue")
x = foo("red")
print(f"x = {x}")

Many people think that the final print statement gets executed multiple times but it does not.

Return statements inside for loops

def foo():
    for i in range(10):
        return i

x = foo()
print(f"x = {x}")

Function arguments/parameters

def foo(*args):
    return args

x = foo()
print(f"x = {x}")

Many people think that the above will print x = None. It will not.

JavaScript specific concepts

Hoisting

foo();

function foo() {
  console.log("hello");
}

Many people assume that the code above will raise an error. It does not.


RAW CONTENT URL