Skip to main content

Function call value uses and benefits in C program #DOTC_Mdb

Function  call value in C program.


Hi Everyone,

Today we will discuss about Function call value in C Program and it’s benefits. In Previous blog we discussed about function call by reference and overview of Function in C program. So before starting Function call value please see my below blogs for your reference.

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

https://dheeraj60.blogspot.com/2020/06/call-by-reference-function-uses-and.html

 

Let’s Start with Function call value : The Function call by value is a method of passing arguments to a function which copy the actual value of an argument into the formal parameter of the function. Changes made to the parameter inside the function have no effect on the argument. In C Program by default it uses call by value to pass arguments. In other word, it means the code within a function cannot alter the arguments used to call the function.

Let’s understand the Actual parameters and Formal parameter to get clear picture on function call value as we use both the parameters in program

Actual parameters that appear in function calls whereas Formal parameters that appears in function declarations.

Understand the Function call value with both the parameter with one simple C Program.

 

#include <stdio.h>

#include <stdlib.h>

int dh(int i)

{

    i = i+1;

    return i;

}

 

int main()

{

   int a=100;

   int b = dh(a);

   printf("\nValue of a is: %d", a);

   printf("\nValue of b is: %d", b);

 

   return 0;

}

 

Here we use function dh and increment the variable by using function , We passed the variable a while calling the method, but since we are calling the function using call by value method, only the value of a is copied to the formal parameter i , so  changes made to the i doesn’t reflect in the variable a. But b will be incremented by passing function by value . So output of a will be the assigned value only that is 100 and value of b is 101.

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

 As you can see the output value of a is 100  and b is 101. 

Let’s understand the same swap program which we used in function call by reference by using Function call value.

Below is the swap program by using function call value.

 

#include <stdio.h>

#include <stdlib.h>

void swap( int a, int b )

{

   int result ;

   result = a ;

   a = b ;

  b = result ;

}

int main( )

{

    int i = 500, j = 1000 ;

    printf("\nBefore swapping the value of i and j is: %d, %d", i, j);

 

    swap(i, j);

    printf("\n  After swapping value of i and j is: %d, %d", i, j);

}

 

Let’s understand this program here we use function swap with integer type variable and in first part of code result =a; is simply Copying a value into temporary variable result  and second a=b copying the value b into and copying temporary variable result into b by using b=result . And finally for swapping we call swap function by using swap(i, j); and we assigned the value of i and j in the program.So by using swap function we can the see the value of variable i and j both before swap which we assigned and swap value by simply using swap function.

 

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

 

Now we can able to see assigned value of variable i and j and also swap value of i and j by using swap function.  Function is called by value for i and j. So actually variable a and variable b gets swapped not variable i and j. As in call by value actual parameters are just copied into the formal parameters. So in this program it shows that there are no changes in the values, though they had been changed inside the function.

 Hope you are able to understand the Function call value in program and also see the differences we used in function call by references for the same swap program we used in previous blog. Please read this blog carefully and try to write some other programs as well and let me know if you have any doubts. In next Blog i will explain Array in Function which is very important topic and we can write some interesting program by using array in Function.

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...

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 que...

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 s...