Module 14 - part 1 and 2
Module 14 - part 1 and 2
• Recursion
• Recursive Functions
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Definition (mathematical)
• Recursion is the process of defining something in terms of itself,
and is sometimes called circular definition.
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Definition (Computer Science)
• A recursive function is one which calls itself.
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Simple Example
main()
{
printf(“This is an example of recursive function”);
main();
}
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Factorial Calculation
int fact(int n) /* non-recursive */
{
int t, ans;
ans = 1;
for(t = 1; t <= n; t++)
ans = ans*t;
return(ans);
}
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
How recursive function works?
• When a function calls itself, a new set of local variables and
parameters are allocated storage on the stack, and the
function code is executed from the top with these new
variables.
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Summing up the elements of
an array
Algorithm Design:
1. If we have only one element, then the sum is simple.
2. Otherwise, we use the sum of the first element and the sum
of the rest.
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Example
For example:
Sum(1 8 3 2) = 1 + Sum(8 3 2) =
8 + Sum(3 2) =
3 + Sum (2) =
2
3+2=5
8 + 5 = 13
1 + 13 = 14
Answer = 14
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Recursive Version of Fibonacci
Series
int fib(int num){
switch(num){
case 0: return 0;
break;
case 1: return 1;
break;
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Analysis
Input Value Number of time fib is called
0 1
1 1
2 3
3 5
4 9
5 15
6 25
7 41
8 67
9 109
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Analysis
factr(4)
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Printing in reverse order
/* This function reads a line of text char by char and then
displays the characters in reverse order */
void Reverse(){
char c;
if((c = getchar())!= ‘\n’)
Reverse();
putchar(c);
}
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Pros and Cons
• The recursive versions of most routines may execute a bit slower
than their iterative equivalents because of the overhead of the
repeated function calls.
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Pros and Cons
• The main advantage to recursive functions is that you can use
them to create clearer and simpler versions of several algorithms.
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Precaution
• When writing recursive functions, you must have an if statement
somewhere to force the function to return without the recursive
call being executed.
• If you don't, the function will never return once you call it.
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Module 14 – part 2:
The C Preprocessor
BITS Pilani Dr. Amitesh Singh Rajput
Pilani Campus
Department of Computer Science & Information Systems
Module Overview
• C Preprocessor
– Preprocessor Directives
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
The C Preprocessor
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
C Program execution
Text editor
C Source Code
Preprocessor
Expanded Source Code
Compiler
Object Code
Linker
Executable Code
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
BITS Pilani
Pilani Campus
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Example 2 (Define Operators)
#define AND &&
#define OR ||
#define EQUALS ==
#define MOD %
#define NOTEQUAL !=
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Example 3 (Define Conditions)
#define AND &&
#define OR ||
#define RANGE ((ch>=’a’ AND ch<=‘z’)OR(ch>=’A’ AND
ch<=‘Z’))
void Alpha_Check(char ch)
{
if(RANGE)
printf(“Entered character is an Alphabet\n”);
else
printf(“Entered character not an Alphabet\n”);
}
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Macros with Arguments
#define CUBE(x) (x*x*x)
Example:
Consider the Macro Definitions
#define SQUARE(p) (p*p)
#define CUBE(p) (SQUARE(p)*p)
#define EIGHTH(p) (CUBE(p)*CUBE(p)*SQUARE(p))
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Nesting of Macros (Contd.)
Macros calls can be nested as function calls
Exercise:
Write a macro call for getting min of five values.
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Advantages of Macros
• One place replacement is required for any changes
• Easy to handle
• Easy to debug
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Macro vs Function()
• Functions make the program compact and smaller but they slow down the
program
• Each call results in a push on the activation stack.
• The corresponding return is a pop from the stack.
• Macros make the program run faster but increases the program size
• Macros reduce the function call overhead by performing code
replacement at compile time.
• Code replacement happens once (per compilation), whereas function calls
can be repetitive causing performance bottleneck.
Important Questions:
When is it best to use macro?
And when is it best to use function?
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Can we replace all Macros with
functions?
• Macros do not have types (for parameters)
• What will happen if we declare
#define fact(n) (n==0) ? 1 : n*fact(n-1)
• Try this out! Recursive substitutions are not possible because the
compiler will not know when to stop.
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
BITS Pilani
Pilani Campus
#include “file_name” OR
#include <file_name>
When a header file is included, the preprocessor includes the file in the
source code program (.c file) before its compilation.
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
BITS Pilani
Pilani Campus