You are on page 1of 1

7. What is a function?

Functions are "self-contained" modules of code that accomplish a specific task. Functions usually
"take in" data, process it, and "return" a result. Once a function is written, it can be used over and
over and over again. Functions can be "called" from the inside of other functions.

19.Give the differences between recursion Iteration


and iteration. Recursion
Function calls itself until the base condition is Repetition of process until the condition fails.
reached.
Only base condition (terminating condition) is It involves four steps: initialization, condition,
specified. execution and updation.
It keeps our code short and simple. Iterative approach makes our code longer.
It is slower than iteration due to overhead of Iteration is faster.
maintaining stack.
It takes more memory than iteration due to Iteration takes less memory.
overhead of maintaining stack.

20. What are advantages and disadvantages Disadvantages


of recursion? Advantages
Recursive functions make the code look clean Sometimes the logic behind recursion is hard to
and elegant. follow through.
A complex task can be broken down into Recursive calls are expensive (inefficient) as
simpler sub-problems using recursion. they take up a lot of memory and time.
Sequence generation is easier with recursion Recursive functions are hard to debug.
than using some nested iteration.

You might also like