Skip to main content

Call by Reference Function uses and Benefits in C Program #DOTC_Mdb


Call by Reference Function in C Program.

 

Hi Everyone,

Today we will discuss about call by Reference Function in C . In my previous blog I explained about overview and benefits of functions in C so before starting this topic please go through below blog so that you can easily understand Call by reference Function.

 https://dheeraj60.blogspot.com/2020/06/overview-of-functions-in-c-program.html

Let’s start with call by reference function in C Program. First thing we need to start the parameters to understand reference function. The parameters are Actual Parameter and Formal parameter. Actual parameter that appears in function call while parameters that appear in function declarations is known as formal parameter.

Let’s understand with one example .

Int add (int i , int j);

Here i and j are formal parameter  and calling the functions as below is Actual parameter.

Int a =add(100,200);

 

So the call by reference method of passing arguments to a function copies the address of an argument into the formal parameter. Inside the function, the address is used to access the actual argument used in the call. It means the changes made to the parameter affect the passed argument. In  other word call by reference, the operation performed on formal parameters, affects the value of actual parameters because all the operations performed on the value stored in the address of actual parameters. Let’s understand this by one simple example of call by reference function in Program to increment the value of variable by 2.

 

#include <stdio.h>

#include <stdlib.h>

void dh(int  *abc)

{

    *abc = *abc+2;

}

int main()

{

     int i=101;

     dh(&i);

     printf("Value of variable i is: %d", i);

 

   return 0;

}

 

Here in above program we function dh and explanation of code is as below.

Here we are performing the increment on variable * abc, however the abc is a pointer that holds the address of variable i, which means the increment is actually done on the address where value of i is stored. In this way of calling the function is known as call by reference. Instead of passing the variable i, we are passing the address of variable i . So for the above program we will get the output 103 as we assigned the value of i is 101 and increment is done  by using *abc = *abc+2;

 

Below is the output of Above code in compiler code-block .

 

Let’s understand with one more program by using reference method function . We use function swap by passing values by reference and get the value before and after swap in main and function value .

Below is the Program.

 

#include <stdio.h>

#include <stdlib.h>

void swap ( int *a, int *b )

{

   int temp ;

   temp = *a ;

   *a = *b ;

   *b = temp ;

}

int main( )

{

   int i = 100 , j= 200 ;

   printf("Before swapping:");

   printf("\ni value is %d", i);

   printf("\nj value is %d", j);

  swap( &i, &j );

  printf("\nAfter swapping:");

   printf("\ni value is %d", i);

   printf("\nj value is %d", j);

   return 0;

}

Let’s understand this program here we use function swap in main we simply get the assigned value of i and j variable that is before swapping and for swap we use calling swap function that is swap( &i, &j ) and printing the After swapping value of variable i and j.

 

Below is the output of above program in compiler code block.

Here we are seeing 4 values of variable i and j with before and after swap value.

Hope you are able to understand this swap program by using swap function call by reference. Please go through this blog carefully as in next blog i will come up with another topic of function that is Function call by value. Please try to write some more programs by using function call by reference and let me know if you have any doubt. In next blog i will come up with Function call by value with programs so please read this blog carefully.

 

Thanks.


Comments

Popular posts from this blog

SQL and Classification of SQL in Oracle Database #Diksha Oracle Training Centre

  SQL and Classification of SQL in Oracle Database.   SQL is Structured Query Language , which is used for storing, manipulating and retrieving data stored in a relational database .SQL is the standard language for RDBMS. All the Relational Database Management Systems (RDMS) like Oracle, MySQL, Sybase, Informix, IBM DB2 and Microsoft SQL Server use SQL as their standard database language. Oracle is one of the more secured database as compared to other databases. Importance of   SQL : SQL and PL/SQL is a backend process where all data is stored and retrieved in GUI which created either by any programming languages like Java, C++, PHP etc. so we need to have very secure database so that there will be no impact for users. SQL allows users to access data in the relational database management systems. SQL is used to communicate with a database.SQL and PL/SQL allows users to create and drop databases tables , views , stored procedures , functions , packages , trigger et...

Basic SQL Commands in Oracle Database #Diksha Oracle Training Centre

  Basic SQL Commands in Oracle Database.   Hello Everyone, In my previous blog I discussed about SQL and Classification of SQL . Today I will discuss about SQL basic commands which widely used in RDBMS. The Topics of SQL command which I am going to cover in this blog are mainly divided into four Categories: ·         DDL:   DDL consists of commands which are used to define and design the database. ·         DML:   DML consists of commands which are mainly used to manipulate the data in the database. ·        DCL: DCL Consists of commands which deal with the user permissions/access and controls in the database. ·       TCL:   TCL Consist of commands which deal with the transaction in the database. If you want to explore theory part please follow my below blog as in today blog I will discuss about how to use the commands. h...

Pseudo Column in Oracle #Diksha Oracle Training Centre

  Pseudo Column in Oracle. Hello Everyone, Today I will discuss about Pseudo column in Oracle. This topic is very important so before going to read this blog, I suggest you to please see my previous blogs which is in archive folder for your reference so that it’s easy for you to understand this topic. Let’s discuss about Pseudo Column in Oracle. Pseudo column: A pseudo-column is dynamic in Oracle as it behaves like a table column but is not stored in the table. Pseudo column behaves like a table column, but is not actually stored in the table. You can select from pseudo columns, but you cannot insert, update, or delete their values. A pseudo column is also similar to a function without arguments. In   Simple word we can say Pseudo column is a column that yields a value when selected, but which is not an actual column of the table. An example is RowID or SysDate. It can be use in combination with the DUAL table. Below are the Pseudo Column commonly used in Oracle Dat...