Functions Flashcards

(7 cards)

1
Q

What is a function in C? What are its benefits?

A

A self-contained block of statements that performs a specific task. Benefits include modularity and code reusability.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Differentiate between Function Declaration, Definition, and Call.

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Explain the difference in scope and lifetime between local and global variables.

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is the purpose of the static storage class?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is a recursive function? What is a crucial part of every recursive function?

A

A function that calls itself to solve a problem. A crucial part is the base case, which stops the recursion.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

When might recursion be preferred over iteration, and vice-versa?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly