INSTITUTE - UIE
DEPARTMENT- ACADEMIC UNIT-2
Bachelor of Engineering (Computer Science &
Engineering)
Subject Name: Fundamentals of Computer Programming
Subject Code: 21CSH-101
CHAPTER NAME -FUNCTIONS
1
COURSE OBJECTIVES
OBJECTIVES
The course aims to provide exposure to problem solving with programming
The course aims to raise the programming skills of students via logic building capability
With the knowledge of C language students would be able to model real world
problems
2
CONTENT
Recursive
Function
References
Examples of
Recursive functions
Macros
3
Definition: The process in which a function calls itself
directly or indirectly is called recursion and the
corresponding function is called as recursive function.
Using recursive algorithm, certain problems can be solved
quite easily.
4
Recursive
Function
Example 1
Recursive
Function
5
Example1: Program to find factorial of a number.
#include <stdio.h>
int factorial(int number);
int main()
{
int x = 9;
printf("The factorial of %d is %dn", x, factorial(x));
return 0;
}
int factorial(int number)
{
if (number == 1)
return (1); /* exiting condition */
else
return (number * factorial(number - 1));
}
Explanation of
Example 1
6
1. We declare our recursive factorial function which takes an integer
parameter and returns the factorial of this parameter. This function
will call itself and decrease the number until the exiting, or the base
condition is reached. When the condition is true, the previously
generated values will be multiplied by each other, and the final
factorial value is returned.
2. We declare and initialize an integer variable with value"6" and
then print its factorial value by calling our factorial function.
7
Example 2
Recursive
Function
Example2: Write a program in C to print first 50 natural numbers
using recursion.
#include<stdio.h>
int numPrint(int);
int main()
{
int n = 1;
printf("nn Recursion : print first 50 natural numbers :n");
printf("-------------------------------------------------n");
printf(" The natural numbers are :");
numPrint(n);
printf("nn");
return 0;
}
int numPrint(int n)
{
if(n<=50)
{
printf(" %d ",n);
numPrint(n+1);
}
}
8
Explanation of
Example 2
Using
Flowchart
9
Macros
Macros: They are basically a fragment of the code that has
been given a name. The name is replaced by the contents of
the macro, whenever the name is used.
A macro is an operation defined in #define preprocessor
directive. As with symbolic constants, the macro-identifier is
replaced in the program with the replacement-text before the
program is compiled.
Macros may be defined with or without arguments. - A macro
without arguments is processed like a symbolic constant while
a macro with arguments, the arguments are substituted in the
replacement text, then the macro is expanded, that is the
replacement-text replaces the identifier and argument list in
the program
10
Example of
Macros
Example: Program to calculate area of circle using macros.
#include
#define PI 3.1415
int main()
{
float radius,area;
printf("Plese Enter the radius ");
scanf("%f", &radius);
//Here used the value of PI
area = PI*radius*radius;
printf("Area=%.2f",area);
return 0;
}
Wherever Pi used in program it is replaced by its value
SUMMARY…..
We covered Recursive
function
Also covered topic Macros
11
FREQUENTLY
ASKED
QUESTIONS
PROGRAMS
1. Write a program in C to calculate the sum of numbers from 1 to n using
recursion.
2. Program of fibonacci program using recursion
3. C program to calculate length of the string using recursion
4. Program to count digits in C using recursion
12
UTILISE YOUR
KNOWLEDGE TO
ANSWER
Let us see how much you
have learned from the lecture
and how effectively you can
apply your knowledge…!!
1. What will be the value retuned by the following function, when it is called
with a value 11?
recur(int num)
if ( ( num/2) !=0 )
return ( recur(num/2 ) * 10+num%2 );
else return 1;
A.Function does not return any value, because it goes into an infinite loop
B.11
C.1011
D. None of the above
2. What will be the output of the following C code?
#include<stdio.h>main()
{ int n; n=f1(4);
printf("%d",n);
}f1(int x)
{ int b; if(x==1)
return 1;
else
b=x*f1(x-1);
return b;}
a) 24
b) 4
c) 12
d) 10
13
UTILISE YOUR
KNOWLEDGE TO
ANSWER
Let us see how much you
have learned from the lecture
and how effectively you can
apply your knowledge…!!
3. In the absence of a exit condition in a recursive function, the
following error is given __________
a) Compile time error
b) Run time error
c) Logical error
d) No error
4. Iteration requires more system memory than recursion.
a) True
b) False
14
• Vedio Lecture:
• https://nptel.ac.in/courses/106/105/106105171/
• http://www.nptelvideos.com/lecture.php?id=6625
• https://www.digimat.in/nptel/courses/video/106105171/L01.html
•
• Websites: https://www.programiz.com/c-programming/c-
recursion
• https://www.cprogramming.com/tutorial/c/lesson16.html
• https://www.phptpoint.com/recursive-function-in-c/
• https://www.tutorialspoint.com/macros-and-preprocessors-in-c
•
•
•
•
•
•
•
•
•
•
REFERENCES
Weblinks
Vedio Lectures
15
THANK YOU….
16

lecture 10 Recursive Function and Macros.ppt

  • 1.
    INSTITUTE - UIE DEPARTMENT-ACADEMIC UNIT-2 Bachelor of Engineering (Computer Science & Engineering) Subject Name: Fundamentals of Computer Programming Subject Code: 21CSH-101 CHAPTER NAME -FUNCTIONS 1
  • 2.
    COURSE OBJECTIVES OBJECTIVES The courseaims to provide exposure to problem solving with programming The course aims to raise the programming skills of students via logic building capability With the knowledge of C language students would be able to model real world problems 2
  • 3.
  • 4.
    Definition: The processin which a function calls itself directly or indirectly is called recursion and the corresponding function is called as recursive function. Using recursive algorithm, certain problems can be solved quite easily. 4 Recursive Function
  • 5.
    Example 1 Recursive Function 5 Example1: Programto find factorial of a number. #include <stdio.h> int factorial(int number); int main() { int x = 9; printf("The factorial of %d is %dn", x, factorial(x)); return 0; } int factorial(int number) { if (number == 1) return (1); /* exiting condition */ else return (number * factorial(number - 1)); }
  • 6.
    Explanation of Example 1 6 1.We declare our recursive factorial function which takes an integer parameter and returns the factorial of this parameter. This function will call itself and decrease the number until the exiting, or the base condition is reached. When the condition is true, the previously generated values will be multiplied by each other, and the final factorial value is returned. 2. We declare and initialize an integer variable with value"6" and then print its factorial value by calling our factorial function.
  • 7.
    7 Example 2 Recursive Function Example2: Writea program in C to print first 50 natural numbers using recursion. #include<stdio.h> int numPrint(int); int main() { int n = 1; printf("nn Recursion : print first 50 natural numbers :n"); printf("-------------------------------------------------n"); printf(" The natural numbers are :"); numPrint(n); printf("nn"); return 0; } int numPrint(int n) { if(n<=50) { printf(" %d ",n); numPrint(n+1); } }
  • 8.
  • 9.
    9 Macros Macros: They arebasically a fragment of the code that has been given a name. The name is replaced by the contents of the macro, whenever the name is used. A macro is an operation defined in #define preprocessor directive. As with symbolic constants, the macro-identifier is replaced in the program with the replacement-text before the program is compiled. Macros may be defined with or without arguments. - A macro without arguments is processed like a symbolic constant while a macro with arguments, the arguments are substituted in the replacement text, then the macro is expanded, that is the replacement-text replaces the identifier and argument list in the program
  • 10.
    10 Example of Macros Example: Programto calculate area of circle using macros. #include #define PI 3.1415 int main() { float radius,area; printf("Plese Enter the radius "); scanf("%f", &radius); //Here used the value of PI area = PI*radius*radius; printf("Area=%.2f",area); return 0; } Wherever Pi used in program it is replaced by its value
  • 11.
  • 12.
    FREQUENTLY ASKED QUESTIONS PROGRAMS 1. Write aprogram in C to calculate the sum of numbers from 1 to n using recursion. 2. Program of fibonacci program using recursion 3. C program to calculate length of the string using recursion 4. Program to count digits in C using recursion 12
  • 13.
    UTILISE YOUR KNOWLEDGE TO ANSWER Letus see how much you have learned from the lecture and how effectively you can apply your knowledge…!! 1. What will be the value retuned by the following function, when it is called with a value 11? recur(int num) if ( ( num/2) !=0 ) return ( recur(num/2 ) * 10+num%2 ); else return 1; A.Function does not return any value, because it goes into an infinite loop B.11 C.1011 D. None of the above 2. What will be the output of the following C code? #include<stdio.h>main() { int n; n=f1(4); printf("%d",n); }f1(int x) { int b; if(x==1) return 1; else b=x*f1(x-1); return b;} a) 24 b) 4 c) 12 d) 10 13
  • 14.
    UTILISE YOUR KNOWLEDGE TO ANSWER Letus see how much you have learned from the lecture and how effectively you can apply your knowledge…!! 3. In the absence of a exit condition in a recursive function, the following error is given __________ a) Compile time error b) Run time error c) Logical error d) No error 4. Iteration requires more system memory than recursion. a) True b) False 14
  • 15.
    • Vedio Lecture: •https://nptel.ac.in/courses/106/105/106105171/ • http://www.nptelvideos.com/lecture.php?id=6625 • https://www.digimat.in/nptel/courses/video/106105171/L01.html • • Websites: https://www.programiz.com/c-programming/c- recursion • https://www.cprogramming.com/tutorial/c/lesson16.html • https://www.phptpoint.com/recursive-function-in-c/ • https://www.tutorialspoint.com/macros-and-preprocessors-in-c • • • • • • • • • • REFERENCES Weblinks Vedio Lectures 15
  • 16.