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

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