What is a function?
Functions are named, reusable sections of a program that perform a focused task. They reduce repetition and make code easier to understand, test, and maintain.
Study 5 Functions and Program Structure with 12 free online flashcards. Review key terms, definitions, and concepts with this interactive flashcard deck.
What is a function?
Functions are named, reusable sections of a program that perform a focused task. They reduce repetition and make code easier to understand, test, and maintain.
What are the main parts of a function?
A function usually has a name, parameters, a body containing instructions, and a return value when it produces a result.
How does a definition differ from a call?
A function definition creates the function; a function call runs it. In Python, the `def` statement introduces a function definition.
What is the difference between a parameter and an argument?
A parameter is a variable listed in a function definition. An argument is the actual value supplied when the function is called.
How does returning differ from printing?
`return` sends a value back to the caller, where it can be stored, displayed, compared, or passed to another function. `print` only displays a value.
What does Python return without an explicit `return`?
In Python, reaching the end of a function without an explicit `return` produces `None`. A function with no useful result therefore still technically returns a value.
What does scope determine?
Scope is the region where a name can be accessed. A local variable normally exists only inside its function, while a global variable is defined at program or module level.
How do local variables support encapsulation?
Encapsulation keeps a function's internal state isolated through local variables. This helps prevent unrelated parts of a program from changing that state accidentally.
What does a function interface specify?
A function's interface includes its name, accepted parameters, possible values or errors, and important side effects such as writing a file or changing a database.
What do cohesion and coupling mean?
High cohesion means a module contains closely related responsibilities. Low coupling means modules depend on one another as little as practical.
What is function decomposition?
Function decomposition breaks a larger problem into focused tasks, gives each task a function, defines its data flow, connects the functions, and tests them independently before testing the whole program.
Why is code reuse valuable?
Code reuse means calling an existing function or module instead of rewriting the same algorithm. It reduces duplication and helps corrections apply consistently.