C : FUNCTIONS


FUNCTIONS



FUNCTION OVERVIEW :

  1. A FUNCTION is a self-contained program segment that carries out some specific, well-defined task.

  2. Every C program consists of one or more functions.

  3. One of these functions must be called main.

  4. Execution of the program will always begin by carrying out the instructions in main.

  5. Additional functions will be subordinate to main, and perhaps to one another.

  6. A FUNCTION will process information that is passed to it from the calling portion of the program, and return a single value.


DEFINING AND ACCESSING A FUNCTION :

  1. DEFINIG A FUNCTION :

    1. SYNTAX :
          
          return_data_type function_name(arguments)
          {
             //STATEMENTS LIST;
             return value;
          }
          
          

    2. C PROGRAMS FUNCTION consists of these parts :

      1. return_data_type :

        A FUNCTION can return a value,the return_data_type is the data_type of tge value which is returned.


      2. function_name :

        The actual name of the FUNCTION which we have defined.


      3. arguments :

        Arguments are nothing but parameters , the value which we pass with the functions in the braces after function name.
        They are OPTIONAL to define.


      4. statements :

        Thay are the tasks to be performed inside the function body.


      5. return :

        It returns the value according to the data types.
        DATA TYPE void returns nothing.



    3. All the other functions in C needs to be defined after main() function.


  2. ACCESSING A FUNCTION :

    1. To use a function that we have declared , we will have to call that function to perform the defined task.

    2.  To call a FUNCTION, we have to pass the required parameters along with the function name, and if the function returns a value, then we can store the returned value.

    3. We can call the FUNCTION using 2 ways : CALL BY VALUE and CALL BY REFERNCE.



//use of a function method 1

#include <stdio.h>

void display()
{
   printf("HELLO");
}

void main()
{
   printf("CALLING DISPLAY FUNCTION");
   display();
   printf("DONE");
}


OR


//use of a function method 2

#include <stdio.h>

void display();

void main()
{
   printf("CALLING DISPLAY FUNCTION");
   display();
   printf("DONE");
}

void display()
{
   printf("HELLO");
}



RECURSIVE FUNCTION :

  1. SYNTAX :
        
        void recursive()
        {
            recursive();
        }
        
        void main()
        {
            recursive();
        }
        
        

  2. The function which call itself,it is called as RECURSIVE FUNCTIONS.

  3. While defining RECURSIVE FUNCTION we also have to define the exit condition.

  4. RECURSIVE FUNCTION will go to INFINE LOOPING unit an exit condition is defined.

  5. This types of FUNCTIONS are mainly used for solving mathematical problems.




//use of recursive function

#include <stdio.h>

int sum(int num)
{
   if(num!=0)
     return num+sum(num-1);
   else
     return num;
}

void main()
{
  int n,result;
  printf("ENTER NUMBER : ");
  scanf("%d",&n);
  result=sum(n);
  printf("RESULT IS : ",result);

}



FUNCTION CALLS :

  1. CALL BY VALUE :

    1. The CALL BY VALUE method of passing ARGUMENTS to a function copies the actual value of an argument into the parameter of the function.

    2. Here changes made to the parameter inside the function have no effect on the argument.

    3. By default, C programming uses CALL BY VALUE to pass arguments. 


  2. CALL BY REFERENCE :

    1. The CALL BY REFERENCE method of passing arguments to a function copies the address of an argument into the parameter.

    2. Inside the function, the address is used to access the actual argument used in the call.

    3. It means the changes made to the parameter affect the passed argument.




//call by value

#include <stdio.h>

void change(int a)
{
   printf("VALUE IS : %d ",a);
   a=a+100;
   printf("VALUE IS : %d ",a);
}

void main()
{
  int x=100;
  printf("ORIGINAL VALUE : %d",x);
  change(x);
  printf("AFTER CALLING A FUNCTION : %d ",x);

}



//call by reference

#include <stdio.h>

void change(int *a)
{
   printf("VALUE IS : %d ",*a);
   *a=a+100;
   printf("VALUE IS : %d ",*a);
}

void main()
{
  int x=100;
  printf("ORIGINAL VALUE : %d",x);
  change(&x);
  printf("AFTER CALLING A FUNCTION : %d ",x);

}



Comments