Forms of function




Types of function


  1. Library function(built-in)-

  2. This are commonly required function grouped together and stored in library comes with library of standered function which present on disk and is written for us by people who write compiler.

    Example: printf();
    scanf(); etc.

  3. User defined function-
  4. These function are wriiten by user (programmer).

    Example:
    showmsg();
    add();

Various Forms Of Function

Function with no argument and no return type:
       #include

       #include

       void add();

          void main()
            {
 		add();
	    }

        void add()
           {
	      int a,b,c;
	      printf("Enter two numbers=");
	      scanf("%d%d",&a,&b);
 		c=a+b;
	      printf("\n Addition=%d",c);
           }


Function with argument and no return type:
       #include

	#include

          void main()
            {
	      int a,b;
	      printf("Enter two number=");
 	       scanf("%d%d",&a&b);
 	       add(a,b);
            }
           void add(int x,int y)
             {
                int c;
	         c=x+y;
	        printf("sum=%d",c);
             }


Function with no argument and return type:
     #include

     #include

      int add();

      void main()
         {
           printf("sum=%d",add()); 
	  }

            int add()
  		{
                   int a,b,c;
	  	   printf("Enter two values=");
		   scanf("%d%d"&a&b);
		   c=a+b;
		    return(c);
		}

Function with argument and return type:
      #include

      #include

        int add(int,int);

          void main()
           {
             int a,b,c;
             printf("Enter value=");
             scanf("%d%d",&a&b);
	     c=add(a,b);
                printf("sum=%d",c);
           }

       int add(int x,int y)
        {

	 return(x+y);

         }


    Uses of function

  1. It avoids rewriting of the same code.

  2. Seperating code into modular function which makes the program easier to design and understand.

  3. Passing values between function.

  4. The machanism is used to pass information to the function is the 'argument'.
  5. The argument are also called as parameter.
      types of argument :

    1. Actual argument:-
    2. These are source of information, calling programs pass actual argument to called function.


    3. Formal argument:-
    4. (i.e parameter)

      The called function access the information using corrosponding formal argument.


    program for function

          
           void main()
    
               {
                 int a,b;
    
    	     printf("Enter two values=");
    
    	     scanf("%d%d",&a&b);            //here a and b are actual argument
    
    	     add(a,b);
    
               }
                  void add(int x,int y)         //here x and y are formal argument
                   {
    
                     printf\n("addition=%d",x+y);
    
                   }   
    
        o/p=Enter two values= 2  3
             addition=5