Functions & parameters (concept)

Computer Science · Programming fundamentals

Question 1 of 10

0

    In Python, a function that reaches its end without a return statement returns:

    Stuck? Read the explanation

    Unlocks at a streak of 5 correct answers in a row.

    Print a free worksheet (PDF)

    Embed this practice on your site or class page

    Paste this HTML into your page. Students practise right there; it is free and needs no account.

    Functions & parameters (concept) explained

    A function is a named block of code that performs one task and can be run whenever it is needed. Parameters are the variables listed in a function's definition, and arguments are the actual values supplied when the function is called. Passing different arguments lets one function work on different data each time.

    Functions avoid repeating the same code in many places and make programs easier to test and change. A function may return a result using a return statement. If it does not return anything, Python gives back the special value None, and Java uses the return type void.

    Parameters are local to the function. They exist only while it runs, and assigning a new value to a parameter does not change the caller's variable, although modifying a list or other mutable object that was passed in is visible to the caller. This is why a function must return its result if the caller needs it.

    def area(width, height):
        return width * height
    
    room = area(4, 3)
    print(room)
    

    Worked example

    Trace the call area(4, 3).

    1. The parameter width receives the argument 4 and height receives 3.
    2. The body computes width * height = 4 * 3 = 12.
    3. return sends 12 back, so room is set to 12.

    The same function can be reused with new arguments: area(5, 2) gives 5 * 2 = 10, with no change to the code.

    What you will practise

    105 multiple-choice questions (Easy, Medium, Hard). Each answer is followed by an explanation.

    Example questions

    • A function is:
    • Parameters are:
    • The return value:
    • Arguments are:
    • Scope of a local variable is:
    • In Python, a function that reaches its end without a return statement returns:
    • Calling a function means:
    • Which keyword defines a function in Python?

    …and 97 more.