SlideShare a Scribd company logo
1 of 16
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

More Related Content

Similar to lecture 10 Recursive Function and Macros.ppt

Similar to lecture 10 Recursive Function and Macros.ppt (20)

Bti1022 lab sheet 9 10
Bti1022 lab sheet 9 10Bti1022 lab sheet 9 10
Bti1022 lab sheet 9 10
 
Function in c program
Function in c programFunction in c program
Function in c program
 
Presentation 2 (1).pdf
Presentation 2 (1).pdfPresentation 2 (1).pdf
Presentation 2 (1).pdf
 
Python Manuel-R2021.pdf
Python Manuel-R2021.pdfPython Manuel-R2021.pdf
Python Manuel-R2021.pdf
 
User Defined Functions in C Language
User Defined Functions   in  C LanguageUser Defined Functions   in  C Language
User Defined Functions in C Language
 
Cp manual final
Cp manual finalCp manual final
Cp manual final
 
function in in thi pdf you will learn what is fu...
function in  in thi pdf you will learn   what                           is fu...function in  in thi pdf you will learn   what                           is fu...
function in in thi pdf you will learn what is fu...
 
c++ exp 1 Suraj...pdf
c++ exp 1 Suraj...pdfc++ exp 1 Suraj...pdf
c++ exp 1 Suraj...pdf
 
Chapter 02 functions -class xii
Chapter 02   functions -class xiiChapter 02   functions -class xii
Chapter 02 functions -class xii
 
C programming session 08
C programming session 08C programming session 08
C programming session 08
 
unit_2 (1).pptx
unit_2 (1).pptxunit_2 (1).pptx
unit_2 (1).pptx
 
CPP Homework Help
CPP Homework HelpCPP Homework Help
CPP Homework Help
 
Unit 2function in python.pptx
Unit 2function in python.pptxUnit 2function in python.pptx
Unit 2function in python.pptx
 
Embedded C - Day 2
Embedded C - Day 2Embedded C - Day 2
Embedded C - Day 2
 
Lecture 1_Functions in C.pptx
Lecture 1_Functions in C.pptxLecture 1_Functions in C.pptx
Lecture 1_Functions in C.pptx
 
Ankita sharma focp
Ankita sharma focpAnkita sharma focp
Ankita sharma focp
 
Datastructure notes
Datastructure notesDatastructure notes
Datastructure notes
 
Function in c
Function in cFunction in c
Function in c
 
Advanced Web Technology ass.pdf
Advanced Web Technology ass.pdfAdvanced Web Technology ass.pdf
Advanced Web Technology ass.pdf
 
Unit-IV.pptx
Unit-IV.pptxUnit-IV.pptx
Unit-IV.pptx
 

More from Abhishekkumarsingh630054

More from Abhishekkumarsingh630054 (7)

UNIT 2 LOOP CONTROL.pptx
UNIT 2 LOOP CONTROL.pptxUNIT 2 LOOP CONTROL.pptx
UNIT 2 LOOP CONTROL.pptx
 
NIFT-Sample-Paper-2020-btech-Programme-GAT.pdf
NIFT-Sample-Paper-2020-btech-Programme-GAT.pdfNIFT-Sample-Paper-2020-btech-Programme-GAT.pdf
NIFT-Sample-Paper-2020-btech-Programme-GAT.pdf
 
PPT DMA.pptx
PPT  DMA.pptxPPT  DMA.pptx
PPT DMA.pptx
 
7. Pointers and Virtual functions final -3.pptx
7. Pointers and Virtual functions final -3.pptx7. Pointers and Virtual functions final -3.pptx
7. Pointers and Virtual functions final -3.pptx
 
Chapter 8 Structure Part 2 (1).pptx
Chapter 8 Structure Part 2 (1).pptxChapter 8 Structure Part 2 (1).pptx
Chapter 8 Structure Part 2 (1).pptx
 
DT-2 Exp 2.3_2.pdf
DT-2 Exp 2.3_2.pdfDT-2 Exp 2.3_2.pdf
DT-2 Exp 2.3_2.pdf
 
ar vr mr certificate.pdf
ar vr mr certificate.pdfar vr mr certificate.pdf
ar vr mr certificate.pdf
 

Recently uploaded

Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Disha Kariya
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...Sapna Thakur
 
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...anjaliyadav012327
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room servicediscovermytutordmt
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 

Recently uploaded (20)

Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
 
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room service
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 

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 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
  • 4. 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
  • 5. 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)); }
  • 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: 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); } }
  • 9. 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. 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
  • 12. 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
  • 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…!! 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 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
  • 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