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

chapter-6 slide.pptx

  • 1.
  • 2.
    Function prototype The declarationof 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 functionaccording 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 noargument 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 argumentbut 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() { floatradius; 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 argumentand 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() { intradius; 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 noargument 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 isa 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 ofa 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 seriesupto 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 ofn 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 ofdigits 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 agiven 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
  • 18.
  • 19.
    PRE PROCESSOR DIRECTIVE Thepre 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 Inclusiondirective 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 substitutiondirective 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 macrosubstitution 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 macrosubstitution 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 tofind 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 macrosubstitution 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