SlideShare une entreprise Scribd logo
1  sur  26
CHAPTER 6
Function
9/11/2022 1
Function prototype
The declaration of a function before using it in
the program is called function prototype. It
tells the compiler in advance about some
characteristics of the function(name of the
function) return type, parameter, etc.
The general format of function prototype is
return type.
Datatype function_name(type arg1, type arg2);
int add(int x, int y);
int add(int x, int, float);
int add();
void add(int, float);
void add();
9/11/2022 2
Classification of function according to
return type and argument:
1. Function with no argument and no return
type
2. Function with argument but no return type
3. Function with argument and return type
4. Function with no argument but with return
type
9/11/2022 3
Function with no argument and no
return type
When a function has no arguments the called
function does not receive any value from the
calling function. Similarly, when the called
function does not return any value , the calling
function does not receive any value from the
called function.
9/11/2022 4
#include<stdio.h>
void circleArea(); // function prototype
main() // Main function
{
circleArea();
}
void circleArea() //user defined function
{
float area;
float radius;
printf("Enter the radius : n");
scanf("%f",&radius);
area = 3.14 * radius * radius ;
printf("Area of Circle is %f",area);
}
9/11/2022 5
Function with argument but no
return type
When a function has arguments, it receives
data from the calling function but when it
does not return any value , the calling function
does not receive any data from the called
function.
9/11/2022 6
#include<stdio.h>
void areaCircle(float rad);
main()
{
float radius;
printf("Enter the radius : n");
scanf("%f",&radius);
areaCircle(radius);
}
void areaCircle(float rad)
{
float ar;
ar = 3.14 * rad * rad ;
printf("Area of Circle = %f",ar);
}
9/11/2022 7
Function with argument and return
type
When a function has arguments the called
function receives data from the calling
function. Similarly, when it returns a value ,
the calling function receives data from the
called function.
9/11/2022 8
#include <stdio.h>
float circleArea(int);
main()
{
int radius;
float a;
printf("Enter the radius of the circle ");
scanf("%d",&radius);
a = circleArea(radius);
printf("n Area of Circle is %f ",a);
}
float circleArea(int r)
{
float area;
area = 3.14 * r * r;
return(area);
}
9/11/2022 9
Function with no argument but with
return type
When a function has no arguments the calling
function does not receive any value from the
calling function but when it returns a value ,
the calling function receive any data from the
called function.
9/11/2022 10
#include<stdio.h>
int areaCircle();
main()
{
float a;
a= areaCircle();
printf("n Area of Circle is %f ",a);
}
int areaCircle()
{
float ar;
float radius;
printf("Enter the radius : n");
scanf("%f",&radius);
ar = 3.14 * radius * radius ;
return ar;
}
9/11/2022 11
Recursive Function:
Recursion is a programming technique that
allows the programmer to express operations
in terms of themselves. It is a programming
method in which function call itself.
Two important condition must be satisfied by
any recursive function and they are:
1. Each time a function call itself, it must be
closer to the solution.
2. There must be decision criteria for stopping
the process which is also called base criteria.
9/11/2022 12
Example: Factorial of a Number Using
Recursion
#include <stdio.h>
long int fact(int n);
int main()
{
int n;
printf("Enter a positive number: ");
scanf("%d", &n);
printf("Factorial of %d = %ld", n, fact(n));
return 0;
}
long int fact(int n)
{
if (n >= 1)
return n*fact(n-1);
else
return 1;
}
9/11/2022 13
Example: Fibonacci series upto nth terms Using Recursion
#include<stdio.h>
int Fibonacci(int);
int main()
{
int n, i;
scanf("%d",&n);
printf("Fibonacci seriesn");
for ( i = 0 ; i <= n ; i++ )
{
printf("%dn", Fibonacci(i));
}
return 0;
}
int Fibonacci(int n)
{
if ( n == 0 )
return 0;
else if ( n == 1 )
return 1;
else
return ( Fibonacci(n-1) + Fibonacci(n-2) );
}
9/11/2022 14
Example: Sum of n natural number using recursion
#include <stdio.h>
int sum (int num);
int main()
{
int num, result;
printf("Enter the number: ");
scanf("%d", &num);
result = sum (num);
printf("Sum of %d natural number is %dn", num, result);
return 0;
}
int sum (int num)
{
if (num != 0)
{
return (sum (num-1)+num);
}
else
{
return 0;
}
}
9/11/2022 15
Example: Sum of digits of a given number using recursion
#include <stdio.h>
int sumDigit (int num);
int main()
{
int num, result;
printf("Enter the number: ");
scanf("%d", &num);
result = sumDigit(num);
printf("Sum of digits in %d is %dn", num, result);
return 0;
}
int sumDigit (int num)
{
if (num != 0)
{
return (num % 10 + sumDigit (num / 10));
}
else
{
return 0;
}
}
9/11/2022 16
Example: reverse a given number using recursive function
#include<stdio.h>
int revFunct(int num);
int main(){
int num,revNum;
printf("Enter any number: n");
scanf("%d",&num);
revNum=revFunct(num);
printf("After reverse the number is :%d n",revNum);
return 0;
}
int rev=0,rem;
revFunct(int num){
if(num){
rem=num%10;
rev=rev*10+rem;
revFunct(num/10);
}
else
return rev;
return rev;
}
9/11/2022 17
Src:https://www.geeksforgeeks.org/storage-classes-in-c/
9/11/2022 20
PRE PROCESSOR DIRECTIVE
The pre processor is a program that process
the source code before it passes through the
compiler. It begins with a ’#’ symbol. The
directive is most often placed at the beginning
of the program before the main function. It
can be classified into two types:
1. File inclusion directive
2. Macro substitution directive
9/11/2022 21
1. File Inclusion directive
This directive can cause one file to be included in
another. The file contains function and macro
definition. The general form of pre-processor
command for file inclusion is :
# include “ file name “
where filename is the name of the file containing
the required definition or function. At this point,
the pre-processor inserts the entire content of
the filename into the program.
Eg:
# include “myfile.c”
# include < myfile.c>
9/11/2022 22
2. Macro substitution directive
It is the process where an identifier in a program is
replaced by a pre-defined string composed of one or more
tokens. This process performs the task under the direction
of predefined statement . They take the general form as:
# define identifier value
Eg: # define PI 3.7415
These are of 3 types:
a. Simple macro substitution
b. Argumented macro substitution
c. Nested macro substituton
9/11/2022 23
a. Simple macro substitution
Simple string replacement is used to define a constant.
#include <stdio.h>
#define num 5
int main()
{
int i ;
float average,arr[n],sum=0;
for (i=0;i<num;i++)
{
scanf("%f",&arr[i]);
sum=sum +arr[i];
}
average= sum/num;
printf ("sum = %f",sum);
printf ("average = %f",average);
return 0;
}
9/11/2022 24
b. Argumented macro substitution
This pre-processor permits us to define macro in more
complex and uniform useful form. The general form is:
# define identifier( a1,a2,a3,......an) expression
where a1,a2,a3,......an are formal macro argument which
are analogous to formal argument in a function definition
There is no space between identifier and opening
parenthesis, string behave like a template.
Subsequent occurrence of a macro call which is analogous
to the function call.
9/11/2022 25
//Example program to find area of a circle
# include <stdio.h>
# define AREA(r) (3.14*r*r)
main()
{
float radius , area ;
printf ("Enter radius");
scanf ("%f",& radius);
area= ( AREA(radius));
printf ( "Area = %f",area);
}
//or alternative program
#include <stdio.h>
#define area(r) (3.141*r*r)
int main()
{
float r=2;
printf("area is %f",area(r));
}
9/11/2022 26
c. Nested macro substitution
One macro can be used in the definition of another macro.
Eg :
# include <stdio.h>
# define N 5
# define LOOP for (i=0; i <N;i++)
main()
{
int i, arr [N], sum=0;
float average;
LOOP
{
scanf("%d",&arr[i]);
sum = sum + arr[i];
}
average = (float)sum /N;
printf ("sum=%f",average);
}
Here macro N is used inside the macro LOOP
9/11/2022 27
Assignment
1. Define function, function definition, function call
,function declaration with example.
2. WAP to find the sum of all the prime numbers in a
given array. The main function should take the help of
user defined function that tests whether the given
number is prime or not.
3. WAP to input a number and find the sum of its digits
using recursive function.
4. Write notes on:
• preprocessor directives
• storage class
9/11/2022 28

Contenu connexe

Similaire à Function Prototypes & Recursion

Similaire à Function Prototypes & Recursion (20)

Dti2143 chapter 5
Dti2143 chapter 5Dti2143 chapter 5
Dti2143 chapter 5
 
functions
functionsfunctions
functions
 
Functions and pointers_unit_4
Functions and pointers_unit_4Functions and pointers_unit_4
Functions and pointers_unit_4
 
7 functions
7  functions7  functions
7 functions
 
1. DSA - Introduction.pptx
1. DSA - Introduction.pptx1. DSA - Introduction.pptx
1. DSA - Introduction.pptx
 
Function in c
Function in cFunction in c
Function in c
 
Function
FunctionFunction
Function
 
Function in C program
Function in C programFunction in C program
Function in C program
 
Function in c
Function in cFunction in c
Function in c
 
Unit 4.pdf
Unit 4.pdfUnit 4.pdf
Unit 4.pdf
 
46630497 fun-pointer-1
46630497 fun-pointer-146630497 fun-pointer-1
46630497 fun-pointer-1
 
function_v1.ppt
function_v1.pptfunction_v1.ppt
function_v1.ppt
 
function_v1.ppt
function_v1.pptfunction_v1.ppt
function_v1.ppt
 
Functions IN CPROGRAMMING OF ENGINEERING.pptx
Functions IN CPROGRAMMING OF ENGINEERING.pptxFunctions IN CPROGRAMMING OF ENGINEERING.pptx
Functions IN CPROGRAMMING OF ENGINEERING.pptx
 
Function recap
Function recapFunction recap
Function recap
 
Function recap
Function recapFunction recap
Function recap
 
Functions in C.pptx
Functions in C.pptxFunctions in C.pptx
Functions in C.pptx
 
Functions struct&union
Functions struct&unionFunctions struct&union
Functions struct&union
 
UNIT3.pptx
UNIT3.pptxUNIT3.pptx
UNIT3.pptx
 
C- Programming Assignment 4 solution
C- Programming Assignment 4 solutionC- Programming Assignment 4 solution
C- Programming Assignment 4 solution
 

Plus de cricketreview

FINAL PRESENTATION.pptx
FINAL PRESENTATION.pptxFINAL PRESENTATION.pptx
FINAL PRESENTATION.pptxcricketreview
 
URBAN DESIGN-lecture-4.pdf
URBAN DESIGN-lecture-4.pdfURBAN DESIGN-lecture-4.pdf
URBAN DESIGN-lecture-4.pdfcricketreview
 
Estimating and costing I Chapter 2.pptx
Estimating and costing I Chapter 2.pptxEstimating and costing I Chapter 2.pptx
Estimating and costing I Chapter 2.pptxcricketreview
 
Chapter 3.4_ Art Deco.pdf
Chapter 3.4_  Art Deco.pdfChapter 3.4_  Art Deco.pdf
Chapter 3.4_ Art Deco.pdfcricketreview
 
3e- Persian Gardens.pptx
3e- Persian Gardens.pptx3e- Persian Gardens.pptx
3e- Persian Gardens.pptxcricketreview
 
chapter-7 slide.pptx
chapter-7 slide.pptxchapter-7 slide.pptx
chapter-7 slide.pptxcricketreview
 
Vernacular Architecture of Nepal.pptx
Vernacular Architecture of Nepal.pptxVernacular Architecture of Nepal.pptx
Vernacular Architecture of Nepal.pptxcricketreview
 
URBAN DESIGN-lecture-6.pdf
URBAN DESIGN-lecture-6.pdfURBAN DESIGN-lecture-6.pdf
URBAN DESIGN-lecture-6.pdfcricketreview
 

Plus de cricketreview (10)

FINAL PRESENTATION.pptx
FINAL PRESENTATION.pptxFINAL PRESENTATION.pptx
FINAL PRESENTATION.pptx
 
DREM_chapter 6.pptx
DREM_chapter 6.pptxDREM_chapter 6.pptx
DREM_chapter 6.pptx
 
URBAN DESIGN-lecture-4.pdf
URBAN DESIGN-lecture-4.pdfURBAN DESIGN-lecture-4.pdf
URBAN DESIGN-lecture-4.pdf
 
Estimating and costing I Chapter 2.pptx
Estimating and costing I Chapter 2.pptxEstimating and costing I Chapter 2.pptx
Estimating and costing I Chapter 2.pptx
 
Chapter 3.4_ Art Deco.pdf
Chapter 3.4_  Art Deco.pdfChapter 3.4_  Art Deco.pdf
Chapter 3.4_ Art Deco.pdf
 
3e- Persian Gardens.pptx
3e- Persian Gardens.pptx3e- Persian Gardens.pptx
3e- Persian Gardens.pptx
 
chapter-4 slide.pdf
chapter-4 slide.pdfchapter-4 slide.pdf
chapter-4 slide.pdf
 
chapter-7 slide.pptx
chapter-7 slide.pptxchapter-7 slide.pptx
chapter-7 slide.pptx
 
Vernacular Architecture of Nepal.pptx
Vernacular Architecture of Nepal.pptxVernacular Architecture of Nepal.pptx
Vernacular Architecture of Nepal.pptx
 
URBAN DESIGN-lecture-6.pdf
URBAN DESIGN-lecture-6.pdfURBAN DESIGN-lecture-6.pdf
URBAN DESIGN-lecture-6.pdf
 

Dernier

CS 3251 Programming in c all unit notes pdf
CS 3251 Programming in c all unit notes pdfCS 3251 Programming in c all unit notes pdf
CS 3251 Programming in c all unit notes pdfBalamuruganV28
 
Comprehensive energy systems.pdf Comprehensive energy systems.pdf
Comprehensive energy systems.pdf Comprehensive energy systems.pdfComprehensive energy systems.pdf Comprehensive energy systems.pdf
Comprehensive energy systems.pdf Comprehensive energy systems.pdfalene1
 
Mine Environment II Lab_MI10448MI__________.pptx
Mine Environment II Lab_MI10448MI__________.pptxMine Environment II Lab_MI10448MI__________.pptx
Mine Environment II Lab_MI10448MI__________.pptxRomil Mishra
 
2022 AWS DNA Hackathon 장애 대응 솔루션 jarvis.
2022 AWS DNA Hackathon 장애 대응 솔루션 jarvis.2022 AWS DNA Hackathon 장애 대응 솔루션 jarvis.
2022 AWS DNA Hackathon 장애 대응 솔루션 jarvis.elesangwon
 
Python Programming for basic beginners.pptx
Python Programming for basic beginners.pptxPython Programming for basic beginners.pptx
Python Programming for basic beginners.pptxmohitesoham12
 
Novel 3D-Printed Soft Linear and Bending Actuators
Novel 3D-Printed Soft Linear and Bending ActuatorsNovel 3D-Printed Soft Linear and Bending Actuators
Novel 3D-Printed Soft Linear and Bending ActuatorsResearcher Researcher
 
TEST CASE GENERATION GENERATION BLOCK BOX APPROACH
TEST CASE GENERATION GENERATION BLOCK BOX APPROACHTEST CASE GENERATION GENERATION BLOCK BOX APPROACH
TEST CASE GENERATION GENERATION BLOCK BOX APPROACHSneha Padhiar
 
Cost estimation approach: FP to COCOMO scenario based question
Cost estimation approach: FP to COCOMO scenario based questionCost estimation approach: FP to COCOMO scenario based question
Cost estimation approach: FP to COCOMO scenario based questionSneha Padhiar
 
Main Memory Management in Operating System
Main Memory Management in Operating SystemMain Memory Management in Operating System
Main Memory Management in Operating SystemRashmi Bhat
 
System Simulation and Modelling with types and Event Scheduling
System Simulation and Modelling with types and Event SchedulingSystem Simulation and Modelling with types and Event Scheduling
System Simulation and Modelling with types and Event SchedulingBootNeck1
 
Immutable Image-Based Operating Systems - EW2024.pdf
Immutable Image-Based Operating Systems - EW2024.pdfImmutable Image-Based Operating Systems - EW2024.pdf
Immutable Image-Based Operating Systems - EW2024.pdfDrew Moseley
 
List of Accredited Concrete Batching Plant.pdf
List of Accredited Concrete Batching Plant.pdfList of Accredited Concrete Batching Plant.pdf
List of Accredited Concrete Batching Plant.pdfisabel213075
 
Comparative study of High-rise Building Using ETABS,SAP200 and SAFE., SAFE an...
Comparative study of High-rise Building Using ETABS,SAP200 and SAFE., SAFE an...Comparative study of High-rise Building Using ETABS,SAP200 and SAFE., SAFE an...
Comparative study of High-rise Building Using ETABS,SAP200 and SAFE., SAFE an...Erbil Polytechnic University
 
OOP concepts -in-Python programming language
OOP concepts -in-Python programming languageOOP concepts -in-Python programming language
OOP concepts -in-Python programming languageSmritiSharma901052
 
Gravity concentration_MI20612MI_________
Gravity concentration_MI20612MI_________Gravity concentration_MI20612MI_________
Gravity concentration_MI20612MI_________Romil Mishra
 
DEVICE DRIVERS AND INTERRUPTS SERVICE MECHANISM.pdf
DEVICE DRIVERS AND INTERRUPTS  SERVICE MECHANISM.pdfDEVICE DRIVERS AND INTERRUPTS  SERVICE MECHANISM.pdf
DEVICE DRIVERS AND INTERRUPTS SERVICE MECHANISM.pdfAkritiPradhan2
 
Paper Tube : Shigeru Ban projects and Case Study of Cardboard Cathedral .pdf
Paper Tube : Shigeru Ban projects and Case Study of Cardboard Cathedral .pdfPaper Tube : Shigeru Ban projects and Case Study of Cardboard Cathedral .pdf
Paper Tube : Shigeru Ban projects and Case Study of Cardboard Cathedral .pdfNainaShrivastava14
 
Katarzyna Lipka-Sidor - BIM School Course
Katarzyna Lipka-Sidor - BIM School CourseKatarzyna Lipka-Sidor - BIM School Course
Katarzyna Lipka-Sidor - BIM School Coursebim.edu.pl
 
Stork Webinar | APM Transformational planning, Tool Selection & Performance T...
Stork Webinar | APM Transformational planning, Tool Selection & Performance T...Stork Webinar | APM Transformational planning, Tool Selection & Performance T...
Stork Webinar | APM Transformational planning, Tool Selection & Performance T...Stork
 
Artificial Intelligence in Power System overview
Artificial Intelligence in Power System overviewArtificial Intelligence in Power System overview
Artificial Intelligence in Power System overviewsandhya757531
 

Dernier (20)

CS 3251 Programming in c all unit notes pdf
CS 3251 Programming in c all unit notes pdfCS 3251 Programming in c all unit notes pdf
CS 3251 Programming in c all unit notes pdf
 
Comprehensive energy systems.pdf Comprehensive energy systems.pdf
Comprehensive energy systems.pdf Comprehensive energy systems.pdfComprehensive energy systems.pdf Comprehensive energy systems.pdf
Comprehensive energy systems.pdf Comprehensive energy systems.pdf
 
Mine Environment II Lab_MI10448MI__________.pptx
Mine Environment II Lab_MI10448MI__________.pptxMine Environment II Lab_MI10448MI__________.pptx
Mine Environment II Lab_MI10448MI__________.pptx
 
2022 AWS DNA Hackathon 장애 대응 솔루션 jarvis.
2022 AWS DNA Hackathon 장애 대응 솔루션 jarvis.2022 AWS DNA Hackathon 장애 대응 솔루션 jarvis.
2022 AWS DNA Hackathon 장애 대응 솔루션 jarvis.
 
Python Programming for basic beginners.pptx
Python Programming for basic beginners.pptxPython Programming for basic beginners.pptx
Python Programming for basic beginners.pptx
 
Novel 3D-Printed Soft Linear and Bending Actuators
Novel 3D-Printed Soft Linear and Bending ActuatorsNovel 3D-Printed Soft Linear and Bending Actuators
Novel 3D-Printed Soft Linear and Bending Actuators
 
TEST CASE GENERATION GENERATION BLOCK BOX APPROACH
TEST CASE GENERATION GENERATION BLOCK BOX APPROACHTEST CASE GENERATION GENERATION BLOCK BOX APPROACH
TEST CASE GENERATION GENERATION BLOCK BOX APPROACH
 
Cost estimation approach: FP to COCOMO scenario based question
Cost estimation approach: FP to COCOMO scenario based questionCost estimation approach: FP to COCOMO scenario based question
Cost estimation approach: FP to COCOMO scenario based question
 
Main Memory Management in Operating System
Main Memory Management in Operating SystemMain Memory Management in Operating System
Main Memory Management in Operating System
 
System Simulation and Modelling with types and Event Scheduling
System Simulation and Modelling with types and Event SchedulingSystem Simulation and Modelling with types and Event Scheduling
System Simulation and Modelling with types and Event Scheduling
 
Immutable Image-Based Operating Systems - EW2024.pdf
Immutable Image-Based Operating Systems - EW2024.pdfImmutable Image-Based Operating Systems - EW2024.pdf
Immutable Image-Based Operating Systems - EW2024.pdf
 
List of Accredited Concrete Batching Plant.pdf
List of Accredited Concrete Batching Plant.pdfList of Accredited Concrete Batching Plant.pdf
List of Accredited Concrete Batching Plant.pdf
 
Comparative study of High-rise Building Using ETABS,SAP200 and SAFE., SAFE an...
Comparative study of High-rise Building Using ETABS,SAP200 and SAFE., SAFE an...Comparative study of High-rise Building Using ETABS,SAP200 and SAFE., SAFE an...
Comparative study of High-rise Building Using ETABS,SAP200 and SAFE., SAFE an...
 
OOP concepts -in-Python programming language
OOP concepts -in-Python programming languageOOP concepts -in-Python programming language
OOP concepts -in-Python programming language
 
Gravity concentration_MI20612MI_________
Gravity concentration_MI20612MI_________Gravity concentration_MI20612MI_________
Gravity concentration_MI20612MI_________
 
DEVICE DRIVERS AND INTERRUPTS SERVICE MECHANISM.pdf
DEVICE DRIVERS AND INTERRUPTS  SERVICE MECHANISM.pdfDEVICE DRIVERS AND INTERRUPTS  SERVICE MECHANISM.pdf
DEVICE DRIVERS AND INTERRUPTS SERVICE MECHANISM.pdf
 
Paper Tube : Shigeru Ban projects and Case Study of Cardboard Cathedral .pdf
Paper Tube : Shigeru Ban projects and Case Study of Cardboard Cathedral .pdfPaper Tube : Shigeru Ban projects and Case Study of Cardboard Cathedral .pdf
Paper Tube : Shigeru Ban projects and Case Study of Cardboard Cathedral .pdf
 
Katarzyna Lipka-Sidor - BIM School Course
Katarzyna Lipka-Sidor - BIM School CourseKatarzyna Lipka-Sidor - BIM School Course
Katarzyna Lipka-Sidor - BIM School Course
 
Stork Webinar | APM Transformational planning, Tool Selection & Performance T...
Stork Webinar | APM Transformational planning, Tool Selection & Performance T...Stork Webinar | APM Transformational planning, Tool Selection & Performance T...
Stork Webinar | APM Transformational planning, Tool Selection & Performance T...
 
Artificial Intelligence in Power System overview
Artificial Intelligence in Power System overviewArtificial Intelligence in Power System overview
Artificial Intelligence in Power System overview
 

Function Prototypes & Recursion

  • 2. Function prototype The declaration of a function before using it in the program is called function prototype. It tells the compiler in advance about some characteristics of the function(name of the function) return type, parameter, etc. The general format of function prototype is return type. Datatype function_name(type arg1, type arg2); int add(int x, int y); int add(int x, int, float); int add(); void add(int, float); void add(); 9/11/2022 2
  • 3. Classification of function according to return type and argument: 1. Function with no argument and no return type 2. Function with argument but no return type 3. Function with argument and return type 4. Function with no argument but with return type 9/11/2022 3
  • 4. Function with no argument and no return type When a function has no arguments the called function does not receive any value from the calling function. Similarly, when the called function does not return any value , the calling function does not receive any value from the called function. 9/11/2022 4
  • 5. #include<stdio.h> void circleArea(); // function prototype main() // Main function { circleArea(); } void circleArea() //user defined function { float area; float radius; printf("Enter the radius : n"); scanf("%f",&radius); area = 3.14 * radius * radius ; printf("Area of Circle is %f",area); } 9/11/2022 5
  • 6. Function with argument but no return type When a function has arguments, it receives data from the calling function but when it does not return any value , the calling function does not receive any data from the called function. 9/11/2022 6
  • 7. #include<stdio.h> void areaCircle(float rad); main() { float radius; printf("Enter the radius : n"); scanf("%f",&radius); areaCircle(radius); } void areaCircle(float rad) { float ar; ar = 3.14 * rad * rad ; printf("Area of Circle = %f",ar); } 9/11/2022 7
  • 8. Function with argument and return type When a function has arguments the called function receives data from the calling function. Similarly, when it returns a value , the calling function receives data from the called function. 9/11/2022 8
  • 9. #include <stdio.h> float circleArea(int); main() { int radius; float a; printf("Enter the radius of the circle "); scanf("%d",&radius); a = circleArea(radius); printf("n Area of Circle is %f ",a); } float circleArea(int r) { float area; area = 3.14 * r * r; return(area); } 9/11/2022 9
  • 10. Function with no argument but with return type When a function has no arguments the calling function does not receive any value from the calling function but when it returns a value , the calling function receive any data from the called function. 9/11/2022 10
  • 11. #include<stdio.h> int areaCircle(); main() { float a; a= areaCircle(); printf("n Area of Circle is %f ",a); } int areaCircle() { float ar; float radius; printf("Enter the radius : n"); scanf("%f",&radius); ar = 3.14 * radius * radius ; return ar; } 9/11/2022 11
  • 12. Recursive Function: Recursion is a programming technique that allows the programmer to express operations in terms of themselves. It is a programming method in which function call itself. Two important condition must be satisfied by any recursive function and they are: 1. Each time a function call itself, it must be closer to the solution. 2. There must be decision criteria for stopping the process which is also called base criteria. 9/11/2022 12
  • 13. Example: Factorial of a Number Using Recursion #include <stdio.h> long int fact(int n); int main() { int n; printf("Enter a positive number: "); scanf("%d", &n); printf("Factorial of %d = %ld", n, fact(n)); return 0; } long int fact(int n) { if (n >= 1) return n*fact(n-1); else return 1; } 9/11/2022 13
  • 14. Example: Fibonacci series upto nth terms Using Recursion #include<stdio.h> int Fibonacci(int); int main() { int n, i; scanf("%d",&n); printf("Fibonacci seriesn"); for ( i = 0 ; i <= n ; i++ ) { printf("%dn", Fibonacci(i)); } return 0; } int Fibonacci(int n) { if ( n == 0 ) return 0; else if ( n == 1 ) return 1; else return ( Fibonacci(n-1) + Fibonacci(n-2) ); } 9/11/2022 14
  • 15. Example: Sum of n natural number using recursion #include <stdio.h> int sum (int num); int main() { int num, result; printf("Enter the number: "); scanf("%d", &num); result = sum (num); printf("Sum of %d natural number is %dn", num, result); return 0; } int sum (int num) { if (num != 0) { return (sum (num-1)+num); } else { return 0; } } 9/11/2022 15
  • 16. Example: Sum of digits of a given number using recursion #include <stdio.h> int sumDigit (int num); int main() { int num, result; printf("Enter the number: "); scanf("%d", &num); result = sumDigit(num); printf("Sum of digits in %d is %dn", num, result); return 0; } int sumDigit (int num) { if (num != 0) { return (num % 10 + sumDigit (num / 10)); } else { return 0; } } 9/11/2022 16
  • 17. Example: reverse a given number using recursive function #include<stdio.h> int revFunct(int num); int main(){ int num,revNum; printf("Enter any number: n"); scanf("%d",&num); revNum=revFunct(num); printf("After reverse the number is :%d n",revNum); return 0; } int rev=0,rem; revFunct(int num){ if(num){ rem=num%10; rev=rev*10+rem; revFunct(num/10); } else return rev; return rev; } 9/11/2022 17
  • 19. PRE PROCESSOR DIRECTIVE The pre processor is a program that process the source code before it passes through the compiler. It begins with a ’#’ symbol. The directive is most often placed at the beginning of the program before the main function. It can be classified into two types: 1. File inclusion directive 2. Macro substitution directive 9/11/2022 21
  • 20. 1. File Inclusion directive This directive can cause one file to be included in another. The file contains function and macro definition. The general form of pre-processor command for file inclusion is : # include “ file name “ where filename is the name of the file containing the required definition or function. At this point, the pre-processor inserts the entire content of the filename into the program. Eg: # include “myfile.c” # include < myfile.c> 9/11/2022 22
  • 21. 2. Macro substitution directive It is the process where an identifier in a program is replaced by a pre-defined string composed of one or more tokens. This process performs the task under the direction of predefined statement . They take the general form as: # define identifier value Eg: # define PI 3.7415 These are of 3 types: a. Simple macro substitution b. Argumented macro substitution c. Nested macro substituton 9/11/2022 23
  • 22. a. Simple macro substitution Simple string replacement is used to define a constant. #include <stdio.h> #define num 5 int main() { int i ; float average,arr[n],sum=0; for (i=0;i<num;i++) { scanf("%f",&arr[i]); sum=sum +arr[i]; } average= sum/num; printf ("sum = %f",sum); printf ("average = %f",average); return 0; } 9/11/2022 24
  • 23. b. Argumented macro substitution This pre-processor permits us to define macro in more complex and uniform useful form. The general form is: # define identifier( a1,a2,a3,......an) expression where a1,a2,a3,......an are formal macro argument which are analogous to formal argument in a function definition There is no space between identifier and opening parenthesis, string behave like a template. Subsequent occurrence of a macro call which is analogous to the function call. 9/11/2022 25
  • 24. //Example program to find area of a circle # include <stdio.h> # define AREA(r) (3.14*r*r) main() { float radius , area ; printf ("Enter radius"); scanf ("%f",& radius); area= ( AREA(radius)); printf ( "Area = %f",area); } //or alternative program #include <stdio.h> #define area(r) (3.141*r*r) int main() { float r=2; printf("area is %f",area(r)); } 9/11/2022 26
  • 25. c. Nested macro substitution One macro can be used in the definition of another macro. Eg : # include <stdio.h> # define N 5 # define LOOP for (i=0; i <N;i++) main() { int i, arr [N], sum=0; float average; LOOP { scanf("%d",&arr[i]); sum = sum + arr[i]; } average = (float)sum /N; printf ("sum=%f",average); } Here macro N is used inside the macro LOOP 9/11/2022 27
  • 26. Assignment 1. Define function, function definition, function call ,function declaration with example. 2. WAP to find the sum of all the prime numbers in a given array. The main function should take the help of user defined function that tests whether the given number is prime or not. 3. WAP to input a number and find the sum of its digits using recursive function. 4. Write notes on: • preprocessor directives • storage class 9/11/2022 28