Showing posts with label user defined function. Show all posts
Showing posts with label user defined function. Show all posts

Built-in Functions

Unit 8. Built-in Functions
        8.1. Mathematical Functions
        8.2. String Functions
        8.3. Character Functions.

Built-in Functions



Function
Function is a program which is written to perform a task.

Functions can be categorized in to two types
1. Built in Functions
     -Mathematical Functions Click here for Math Function (Inbuilt)
     -String Functions Click here for example
     -Character Functions Click here for example
2. User Defined Functions

Built in functions are those which are already available in C Library.They can be used in any program for example printf(), scanf(),ceil(),strcpy() etc

User defined function is function written by the user.

main() is also a user defined function.





Built-in Functions
1.Mathematical Functions

Library  #include<math.h>

1.abs()
abs() function returns the absolute value of the integer parameter.The absolute value of integer is always positive.

Syntax:
        int abs(int a);
Example:
          int a=-30;
       printf("Absolute value of a = %d\n", abs(a));
       Absolute value of a =30

for floating values it is fabs()

2. ceil()
ceil() function return nearest value which is greater than or equal to the argument passed.
Syntax:
double ceil(double a)

Example:
       float i=5.2;       printf("ceil of  %f is  %f\n", i, ceil(i));

       ceil of 5.200000 is 6.000000


3. floor()
floor() function return nearest value which is less than or equal to the argument passed.
Syntax:
        double floor(double a)
Example:
       float i=5.2;       printf("floor of  %f is  %f\n", i, floor(i));

       ceil of 5.200000 is 4.000000

4. pow()
pow() function return the power of the number.
Syntax:
        double pow(double base,double exp)
Example:
       printf("4 power 2= %f\n",pow(4,2));

       4 power 2= 16.000000

5. log10()
log10() function return value of base 10 algorithm.

Syntax:
         double log10(double a)

Example:
       float a=10.0;       printf("The value of log(%f) : %f \n", a,log10(a));

       The value of log(10.000000) : 1.000000

6. sqrt()
sqrt() function will return the square root of the given number
Syntax:
        double sqrt(double a)

Example:
       printf ("sqrt of 9 = %f\n", sqrt (9) );

          sqrt of 9 = 3.000000

7. exp()
exp() function will calculate the exponential value of the power.
Syntax:
         double exp(double a)

Example:
       float a=1.0;       printf("The value of exp(%f) : %f \n", a,exp(a));

       The value of exp(1.000000) : 1E

8. sin()

9.cos()

10.tan()

Sin() cos() and tan() are used to calculate the sine, cosine and tangent values.
Example: 
              float j=45.0;

             printf("The value of sin(%f) : %f \n", j, sin(j)); 
             printf("The value of cos(%f) : %f \n", j, cos(j));
             printf("The value of tan(%f) : %f \n", j, tan(j));


       The value of sin(45) : 0.850903

       The value of cos(45) : 0.525321

       The value of tan(45) : 1.619775

User Defined Function (UDF)

User Defined Functions

A function is a block of code designed to perform a particular task. User-defined functions are functions which are defined by the user. Functions are made for code reusability and for saving time and space.

Every user defined function has three parts as:

1.       Prototype or Declaration
2.      Calling
3.      Definition

1. Prototype or Declaration
Every function in C programming should be declared before they are used.
This declaration is called function prototype.
Function prototype gives information to compiler about function name, total number of arguments and type of arguments to be passed and return type.

Syntax:
return_data_type function_name(data_type parameter1, data_type parameter2 ......);

In above syntax "return_data_type" means return value of function:
if return type is void then function will return nothing if return type is int,float char then it will return as type declared.
In above syntax "function_name" means that the name of function which we want to define.

Example:
void display(int a,int b);     //return nothing
int sum(int a,int b);                        //return integer
float avg(float, int);             //return float

2.Calling
The calling part of function work as:
    It is call the function.
    It forward the control from main() to user define function and return back to main again.

Syntax:
function_name(actual argument/parameter);

Example:
display(a,b); //calling void function return nothing

int s;
s=sum(a, b);                         //calling integer function return integer

float a;
a=avg(f, i);                 //calling float function return float

3.Definition
The "definition" part is the code of function.
What will function do? It carry the code we want to write in function.
The definition part must be outside of main() function as we can not define function inside another function.

syntax:
//if return type is void:
function_name(data_type argument1, data_type argument2…/parameter)
{
   statement 1;
   statement 2;
          .
   statement n;
}
Example:
void sum(int a, int b)
{
            int s=a+b
printf(“sum of a+b=%d”,s);
}

//Function with void type
#include<stdio.h>
#include<conio.h>

void sum(int , int);
//Function Prototype/Declaration
void main()
{
            int x,y;
            clrscr();
            printf("Enter value of x and y: ");
            scanf("%d %d", &x, &y);
            sum(x, y);                 
            //calling of function-sum
            getch();
}
//Definition of function sum
void sum(int a, int b)
{
            int res;
            res = a + b;
            printf("Sum of %d + %d = %d",a,b,result);
}
//Function with int type
#include<stdio.h>
#include<conio.h>

int sum(int , int);
//Function Prototype/Declaration
void main()
{
            int x,y,result;
            clrscr();
            printf("Enter value of x and y: ");
            scanf("%d %d", &x, &y);
            result=sum(x, y);                                         //calling of function-sum
            printf("Sum of %d + %d = %d",x,y,result);
            getch();
}
//Definition of function sum
int sum(int a, int b)
{
            int res;
            res = a + b;
            return res;
}


Category/Type of Function
User-defined functions can be categorized as:

1.       Function with no arguments and no return value
2.      Function with no arguments and return value
3.      Function with arguments but no return value
4.      Function with arguments and return value

1.      Function with no arguments and no return value

#include<stdio.h>
#include<conio.h>

void sum( );
//Function Prototype/Declaration with no arguments and no return value
void main()
{
      clrscr();
      sum(x, y);                 
      //calling of function-sum
      getch();
}
//Definition of function sum
void sum( )
{
      int x,y,res;    
      printf("Enter value of x and y: ");
      scanf("%d %d", &x, &y);
      res = a + b;
      printf("Sum of %d + %d = %d",a,b,result);
}


2.     Function with no arguments and return value

#include<stdio.h>
#include<conio.h>

int sum( );  //Function Prototype/Declaration with no arguments and return value
void main()
{
      int result;
      clrscr();         
      result=sum( );                     
      //calling of function-sum
      printf("Sum of x + y = %d", result);
      getch();
}
//Definition of function sum
void sum( )
{
      int x,y,res;
      printf("Enter value of x and y: ");
      scanf("%d %d", &x, &y);
      res = a + b;
      return res;   //returning integer value
}


3.     Function with arguments and no return value

#include<stdio.h>
#include<conio.h>

void sum(int , int);
//Function Prototype/Declaration with arguments and no return value
void main()
{
      int x,y;
      clrscr();
      printf("Enter value of x and y: ");
      scanf("%d %d", &x, &y);
      sum(x, y);                 
      //calling of function-sum
      getch();
}
//Definition of function sum
void sum(int a, int b)
{
      int res;
      res = a + b;
      printf("Sum of %d + %d = %d",a,b,result);
}


4.     Function with arguments and return value

#include<stdio.h>
#include<conio.h>

int sum(int , int);  //Function Prototype/Declaration with arguments and return value
void main()
{
      int x,y,result;
      clrscr();
      printf("Enter value of x and y: ");
      scanf("%d %d", &x, &y);
      result=sum(x, y);                                         //calling of function-sum
      printf("Sum of %d + %d = %d",x,y,result);
      getch();
}
//Definition of function sum
int sum(int a, int b)
{
      int res;
      res = a + b;
      return res;
}


Recursion

Recursion is the process of repeating items in a self-similar way. In programming languages, if a program allows you to call a function inside the same function, then it is called a recursive call of the function.

void recursion()
{
   recursion(); /* function calls itself */
}

void main()
{
   recursion();
}

The C programming language supports recursion, i.e., a function to call itself. But while using recursion, programmers need to be careful to define an exit condition from the function,
Otherwise it will go into an infinite loop

//Fibonacci series using recursion function

#include<stdio.h>
#include<conio.h>

int fibo(int n);

void main()
{
            int n, i;

            printf("\n Enter how many no of terms you want of Fibonacci series : ");
            scanf("%d", &n);
           
            printf("\nFibonacci Series of %d terms is as follows: \n",n);
            for(i=1;i<=n;i++)
            {
                        printf("%5d", fibo(i));
            }

            getch();
}


int fibo(int n)
{
            int r;
            if(( n == 1) || ( n == 0))
                        return 0;
            else if(n == 2)
                        return 1;
            else
                        return (fibo(n-1) + fibo(n-2));
}


//Write a program to find out factorial of a number using recursion funciton
#include<stdio.h>
#include<conio.h>

int fact(int n);
void main()
{
            int n,f1;
            printf("\nENTER N:");
            scanf("%d",&n);
           
            f1 = fact(n);
           
            printf("\nN = %d",n);
            printf("\nFact = %d",f1);

            getch();
}

int fact(int n)
{
            int f;
            if(n==1)
                        return 1;
            else
                        f=n*fact(n-1);
            return f;
}

Passing array to Function

Like simple variable it is possible to pass the array to the function. To pass array to the function it requires the name of the array and size of the array like

                        display (ar,n); //Where ar is the name of array and n is size of an array

It will pass the whole array ar to the function display. The function definition will be like

                        void display(in ar[],int size) //Definition

The function display is defined with two arguments, the name of array and the size of the array. It is not necessary to specify the size of array here.

//Example of passing array to function
#include<stdio.h>
#include<conio.h>
void display(int ar[],int n); //function prototype
void main()
{
            int ar[5],i;
            clrscr();
            printf("Insert data");
            for(i=0;i<5;i++)
            {
                        scanf("%d",&ar[i]);
            }
            display(ar,5); //passing array in function
            getch();
}
void display(int ar[],int size) //function definition
{
            for(i=0;i<5;i++)
            {
                        printf("\n %d",ar[i]);
            }
}

Same way we can also pass the string to the function
//Example of passing string to array
#include<stdio.h>
#include<conio.h>
void display(char name[]);
void main()
{
            char name[20];
            clrscr();
            printf("Insert name");
            scanf("%s",name);
            display(name);
            getch();
}
void display(char name[])
{
            int l;
            l=strlen(name);
            printf("\nstring:%s\nlength:%d",name,l);
}