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 etc. SQL allows

Materialized View uses and Benefits in Database #DOTC_Mdb

Materialized View uses and Benefits in Database. Hello Everyone, Today we will discuss about Materialized view as it’s play important role in database. We already discussed about Simple Views and complex views in my previous blog. Before Materialized view go through my previous blog which related to simple view. https://dheeraj60.blogspot.com/2020/05/benefits-of-creating-and-using-view-in.html As we know View is not a database object and not like table which is stored in database, but view can be created from base table a view is an SQL statement that’s stored in the database. This statement, or view, has a name.A view looks and acts a lot like a table. It has columns and rows, and can be included in SELECT queries just like a table. In other word we can say View is a virtual/logical table which is basically used for security purpose. Let’s understand   about   Materialized view : A materialized view is a view that stores the results of the view’s query. Whenever you query the ma

Top 50 Interview Questions On SQL and PL/SQL #DOTC_Mdb

                    Top 50 Interview Questions On SQL and PL/SQL. Today we will Discuss Top 50 interview questions and answers of SQL and PL/SQL which is frequently asked in interview.     Question 1: What is SQL and Classification of SQL? Answer SQL is a Structure Query Language which is vastly used in RDBMS database like Oracle, Sybase, DB2 , Microsoft SQL server etc.   Classification of SQL is as below: DDL (Data Definition Language):  Commands are  create , alter , drop , truncate etc DML (Data Manipulation Language) : Commands are  insert,update and delete . TCL (Transaction Control Language ) : Commands are  Commit , Rollback and Save point. DCL (Data Control Language) : Commands are Grant , Revoke Question 2:    What is meant by Joins? What are the types of join? Answer Joins are basically used to extract/get data from multiple tables using some common columns or conditions and also by using alias to fulfill the condition.   There are various types of Joins as li