What is a function and how does it improve code reuse? Provide a simple example signature.

Study for the End of Year 8 Computer Science Test. Prepare with flashcards and multiple choice questions, each with hints and explanations. Get ready for your exam!

Multiple Choice

What is a function and how does it improve code reuse? Provide a simple example signature.

Explanation:
A function is a named block of code that performs a specific task and can be run from different places in a program by passing in inputs (parameters) and, usually, returning a result. This setup lets you reuse the same logic without repeating code—write it once, then call it with different values to get different outcomes. A simple signature in Python shows this idea clearly: def add(a, b): return a + b. Here, add is the function name, a and b are the inputs, and it returns their sum, showing how the same block of code can be reused with different numbers. Other constructs described in the options aren’t about reusable blocks of logic with parameters: a class defines a blueprint for objects, a memory location stores a value (like a variable), and a loop repeats code but doesn’t encapsulate reusable logic that can be invoked with different inputs.

A function is a named block of code that performs a specific task and can be run from different places in a program by passing in inputs (parameters) and, usually, returning a result. This setup lets you reuse the same logic without repeating code—write it once, then call it with different values to get different outcomes. A simple signature in Python shows this idea clearly: def add(a, b): return a + b. Here, add is the function name, a and b are the inputs, and it returns their sum, showing how the same block of code can be reused with different numbers.

Other constructs described in the options aren’t about reusable blocks of logic with parameters: a class defines a blueprint for objects, a memory location stores a value (like a variable), and a loop repeats code but doesn’t encapsulate reusable logic that can be invoked with different inputs.

Subscribe

Get the latest from Passetra

You can unsubscribe at any time. Read our privacy policy