Sunday, 21 January 2018

Parameter Passing Technique


Parameter Passing Technique

Parameter Passing Technique
The parameter passing mechanism refers to the actions taken regarding the parameters when a function is invoked. There are two parameter passing techniques available in C.
They are:

1) Call By Value
2) Call By Refernce

Call by value:
      Whenever a portion of the program invokes a function with formal arguments, control will be transferred from the main to the calling function and the value of the actual argument is copied to the function. within the function,the actual value copied from the calling portion of the program may be altered or changed. when the control is transferred back from the function to the calling portion of the program, the altered values are not transferred back. This type of passing formal arguments to a function is technically known as "Call by value".




Ex: A program to exchange the contents of two variables using
a call by value.

   #include
    main()
    {
      int x,y;
      void swap(int,int)
      x=10;
      y=20;
      printf("values before swap()\n");
      printf("x=%d and y=%d",x,y);
      swap(x,y);          /*call by value*/
      printf("values after swap()\n");
      printf("x=%d and y=%d ",x,y);
    }
    void swap(int x ,int y)   /*values will not be swapped */
    {
     int temp;
     temp=x;
     x=y;
     y=temp;
    }

Output of the above program:

     values before swap()
        x=10 and y=20
     values after swap()
        x=10 and y=20

Call by Reference:

       When a function is called by a portion of program, the address of the actual arguments are copied onto the formal arguments, though they may be referred by different variable names.The content of the variables that are altered with in the function block are returned to the calling portion of a
program in the altered form itself, as the formal and actual arguments are referring the same memory location or address. This is technically known as call by reference or call by
address.

Ex: A program to exchange the contents of two variables using
  a call by reference.

  #include
   main()
   {
    int x, y;
    void swap(int *x, int *y);
    x=10; y=20;
    printf("values before swap() \n");
    printf("x=%d and y=%d \n",x,y);
    swap(&x,&y);       /* call by reference */
    printf("values after swap() \n");
    printf("x=%d and y=%d \n",x,y);
   }
    void swap(int *x ,int *y)   /*values will  be swapped */
    {
      int temp;
      temp=*x;
      *x=*y;
      *y=temp;
    }

Output of the above program:

     values before swap()
         x=10 and y=20
     values after swap()
         x=20 and y=10

Related Posts:

  • Enumerated Data Type Enumerated Data Type     This is one of the user defined datatypes. The use of this datatype is to make programs more readable.It allows you to create your own datatype with predefined values. Though its form … Read More
  • Defining And Opening A File Defining And Opening A File  If we want to store data in a file in the secondary memory, we must specify certain things about the file , to the operating system. They include:  1.File name.  2.Data Structu… Read More
  • Difference Between The Macros And Functions Difference Between The Macros And Functions Macros are like functions but there is difference between those two . 1. In macro call the processor replaces the macro template with its macro expansion. Where as in functi… Read More
  • Closing A File & File Functions Closing A File & File Functions When we have finished all operations the file must be closed . This ensures that all outstanding information associated with the file is flushed out from the buffers and all links to… Read More
  • Inroduction To PreProcessors Inroduction To PreProcessors INTRODUCTION:     The C preprocessor is a program that processes our source program before it is passed to the compiler.This is a  capability that does not exist in many other … Read More

0 comments:

Post a Comment