Functions Flashcards
(7 cards)
What is a function in C? What are its benefits?
A self-contained block of statements that performs a specific task. Benefits include modularity and code reusability.
Differentiate between Function Declaration, Definition, and Call.
Declaration (Prototype): Tells the compiler about the function’s signature (name, return type, parameters).
Definition: Contains the actual code (body) that performs the function’s task.
Call: Invoking a function to execute its code.
Explain the difference in scope and lifetime between local and global variables.
Local Variables: Declared inside a function/block; accessible only within that block; created on entry, destroyed on exit.
Global Variables: Declared outside all functions; accessible from anywhere in the program; exist throughout program execution.
What is the purpose of the static storage class?
For local variables, static makes their values persist between function calls and initializes them to zero by default. For global variables/functions, it restricts their visibility to the current file.
What is a recursive function? What is a crucial part of every recursive function?
A function that calls itself to solve a problem. A crucial part is the base case, which stops the recursion.
When might recursion be preferred over iteration, and vice-versa?
Recursion: Often more concise for problems with recursive structures (e.g., tree traversals, Tower of Hanoi). Can use more memory and be slower due to function call overhead.
Iteration: Generally more efficient in terms of memory and time.