logo

AQA A-Level Computer Science practice

Write a Python 3 subroutine called larger that accepts two numbers and returns the larger number. Then write a statement in the calling routine that stores the returned value in a variable called result. If the two numbers are equal, returning either number is acceptable.

Try the question first, then check the answer and practise the same topic again.

At a glance

Question

Type

practice

Style

Topic

Programming

Exam-style question

Try this first

Write a Python 3 subroutine called larger that accepts two numbers and returns the larger number. Then write a statement in the calling routine that stores the returned value in a variable called result. If the two numbers are equal, returning either number is acceptable.

Answer and explanation

Concise answer

def larger(first, second): if first >= second: return first else: return second

Why this is correct

The subroutine compares the two parameters. It returns first when first is greater than or equal to second; otherwise it returns second. The calling routine uses larger(12, 9), receives 12, and stores that returned value in result.

Use the concise answer above for a quick check, then reveal the full model answer and detailed explanation.

Common mistake

No common mistake is linked to this question yet.

Related flashcards

No flashcards are published for this page yet.

Related practice questions