Functions in C Language

 

C Functions


Function is a collection of statements used to perform specific operation.In C Language these function are classified into following types
  1. predefined Function
  2. UserDefined Function


PreDefined Functions

    If any function already designed by the C Develophers is known as predefined functions .These are classified into following types
    1. printf()
    2. Scanf()
    3. getch()
    4. clrscr()
    5. sizeof() ...ete

UserDefined Function

If any function designed by the programmer is known as user defined function
Syntax
  returntype functionname(list of paramers/no parameters);    //function Declaration
  returntype functionname(list of paramers/no parameters)      //function Body
  {
  ....
  ....
  ....
  ....
  }
  
in the above syntax first you declare the function.Ofter you declare the function body
These functions are classified into fallowing types
  1. Parametarized Function
  2. NoParametarized Function
  • Parametarized Function:
  • If any function signature contains list of parameters is known as Parametarized Function.
    Syntax
      returntype functionname(datatype variable1,datatype variable2,....);    //function Declaration
      returntype functionname(datatype variable1,datatype variable2,....)      //function Body
      {
      ....
      ....
      ....
      ....
      }
      
  • NoParametarized Function:
  • If any function signature does not contains list of parameters is known as NoParametarized Function.
    Syntax
      returntype functionname();    //function Declaration
      returntype functionname()     //function Body
      {
      ....
      ....
      ....
      ....
      }
      
    Based on the return type these functions are classified into following types
    1. No Returntype With NoParametarized Function
    2. No Returntype With Parametarized Function
    3. Returntype With NoParametarized Function
    4. Returntype With Parametarized Function
  • No Returntype With NoParametarized Function
  • #include<stdio.h>
    #include<conio.h>
    void max();
    void max()
    {
    int x,y;
    printf("Enter x,y values");
    scanf("%d%d",&x,&y);
    if(x>y)
    {
    printf("x is max");
    }
    else
    {
    prinf("y is max");
    }
    };
    void main()
    {
    clrscr();
    max();
    getch();
    }
     


  • No Returntype With Parametarized Function
  • #include<stdio.h>
    #include<conio.h>
    void max(int x,int y);
    void max(int x,int y)
    {
    
    if(x>y)
    {
    printf("x is max");
    }
    else
    {
    prinf("y is max");
    }
    };
    void main()
    {
    clrscr();
    int x,y;
    printf("Enter x,y values");
    scanf("%d%d",&x,&y);
    max(x,y);
    getch();
    }
     


  • Returntype With NoParametarized Function
  • #include<stdio.h>
    #include<conio.h>
    int max();
    int max()
    {
    int x,y;
    printf("Enter x,y values");
    scanf("%d%d",&x,&y);
    if(x>y)
    {
    return x;
    }
    else
    {
    return y;
    }
    };
    void main()
    {
    clrscr();
    printf("%d",max());
    getch();
    }
     


  • Returntype With Parametarized Function
  • #include<stdio.h>
    #include<conio.h>
    int  max(int x,int y);
    int  max(int x,int y)
    {
    
    if(x>y)
    {
    return x;
    }
    else
    {
    return y;
    }
    };
    void main()
    {
    clrscr();
    int x,y;
    printf("Enter x,y values");
    scanf("%d%d",&x,&y);
    printf("%d",max(x,y));
    getch();
    }
     


    Function Calling

    Function Calling Syntax :
    functionname(arguments,...);
    Example: max(10,20)
    Here Max is a functionname
    10,20 are the arguments

    C - 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.
    Syntax :
     void recursion() {
       recursion(); /* function calls itself */
    }
    
    int main() {
       recursion();
    }
     
    #include<stdio.h>
    #include<conio.h>
    int factorial(int i) {
    
       if(i <= 1) {
          return 1;
       }
       return i * factorial(i - 1);
    }
    
    void  main() {
       int i = 5;
       printf("Factorial of %d is %d//n", i, factorial(i));
       getch();
    }
    

    No comments:

    Post a Comment