Compare the Difference Between Similar Terms

Difference Between

Home / Technology / IT / Programming /调用由参考价值和调用之间的区别ence

调用由参考价值和调用之间的区别ence

December 26, 2017Posted byLithmee

Key Difference – Call by Value vs Call by Reference

Many programming languages use functions. Afunctionis a set of statements to perform a specific task. The main code can be divided into several functions and call them. There are two ways of calling a function such as call by value and call by reference. In call by value method, copies of variables are passed into the function. If there is a change in the value of the function, it does not change the original value. In call by reference, changes of the variable inside the function reflect the original value. Thekey differencebetween call by value and call by reference is that, in call by value, the copy of variables are passed to the function and in call by reference, the addresses of the variables are passed to the function. This article discusses the difference between call by value and call by reference.

CONTENTS

1.Overview and Key Difference
2.What is Call by Value
3.What is Call by Reference
4.Similarities Between Call by Value and Call by Reference
5.Side by Side Comparison – Call by Value vs Call by Reference in Tabular Form
6.Summary

What is Call by Value?

In call by value, copies of variables are passed to the function. The function may change that copied value but it does not change the original value. This concept can easily be understood by swapping two numbers program. Refer the belowCprogram written using Code Blocks environment.

调用由参考价值和调用之间的区别ence_Figure 01

Figure 01: Program to Swapping two Numbers

According to the program given below, the variable ‘a’ has value 20 and variable ‘b’ has value 30. When calling swap(a,b) function, these two values are passed to the swap function. In theswap function, ‘a’ is copied into ‘x’ and ‘b’ is copied into ‘y’. Now, ‘x’ is having value 20 and ‘y’ is having value 30. Using the temp variable the two values are swapped. When printing the value of ‘x’ and ‘y’ inside that swap function, the output will give 30 for ‘x’ and 20 for ‘y’. When the swap function execution is over, the control returns back to the main function. When printing the values of ‘a’ and ‘b’, in the main function, the results will be the original values. They are 20 for ‘a’ and 30 for ‘b’. Changes made using swap function does not reflect in the main program.

调用由参考价值和调用之间的区别ence_Figure 02

Figure 02: Output of the Swapping Program

‘x’ and ‘y’ inside the swap function has the swapped values which are 30 and 20 but those values cannot be used in the main program. Even though ‘x’ and ‘y’ are having swapped values, they are local variables to swap function and cannot be accessed by the main function. To avoid this problem, call by reference can be used.

引用调用是什么?

In this method, addresses of the variables are sent to the function. Refer the below C program written using Code Blocks environment.

Key Difference Between Call by Value and Call by Reference

Figure 03: Swapping of two Numbers using Pointers

According to the given example below, the value of ‘a’ is 20 and value of ‘b’ is 30. Instead of passing copies of ‘a’ and ‘b’, the programmer can send the addresses of ‘a’ and ‘b’ using the function, swap(&a, &b).

Key Difference Between Call by Value and Call by Reference_Figure 04

Figure 04: Output of the Swapping Program using Pointers

In the swap, the function should usepointersbecause addresses are passed and those addresses should be held by pointers. Inside the function, the address of ‘a’ is copied to ‘x’ and address of ‘b’ is copied to ‘y’. Using the temp variable, ‘x’ and ‘y’ values are swapped. In this call by the reference method, the changes inside the swap function reflect in the main program. Printing the values of ‘a’ and ‘b’ in the main will give the swapped values. Now the output of ‘a’ is 30 and ‘b’ is 20. As the function is taking the address of the variables, the changes made inside the function affects the original values.

What is the Similarity Between Call By Value and Call By Reference?

  • These two methods are used for calling functions/methods.

的区别是什么Between Call By Value and Call By Reference?

Call By Value vs Call By Reference

In call by value, copies of variables are passed into the function so changes made inside the function, will not modify the original value. In call by reference, the address of the variables is passed to the function, so changes made for variables inside the function, will modify the original value.
Value Modification
In call by value, the original value is not changing. In call by reference, the original values are changing.

Summary –Call by Value vs Call by Reference

Call by value and call by reference are methods of calling the function. The difference between call by value and call by reference is that in the call by value the copies of variables are passed to the function and in the call by reference, the addresses of the variables are passed to the function. Using call by value or call by reference depends on the task to perform.

Download the PDF Version of Call by Value vs Call by Reference

You can download PDF version of this article and use it for offline purposes as per citation note. Please download PDF version here调用由参考价值和调用之间的区别ence

Reference:

1.Jaiswal, Haresh . “11 Call by Value vs Call by Reference.”YouTube, YouTube, 24 Dec. 2015.Available here

Related posts:

Difference Between Objects and Classes Difference Between Prolog and Lisp Difference Between Abstract Class and Interface Difference Between Bubble Sort and Selection Sort Difference Between White-Box and Black-Box Testing

Filed Under:ProgrammingTagged With:Call by Reference,Call by Reference Definition,Call by Reference Value Modification,Call by Value,Call by Value and Call by Reference Differences,Call by Value and Call by Reference Similarities,Call by Value Definition,Call by Value Value Modification,Call by Value vs Call by Reference,Compare Call by Value and Call by Reference

About the Author:Lithmee

Lithmee Mandula is a BEng (Hons) graduate in Computer Systems Engineering. She is currently pursuing a Master’s Degree in Computer Science. Her areas of interests in writing and research include programming, data science, and computer systems.

Leave a ReplyCancel reply

Your email address will not be published.Required fields are marked*

Request Article

Featured Posts

Difference Between Coronavirus and Cold Symptoms

Difference Between Coronavirus and Cold Symptoms

Difference Between Coronavirus and SARS

Difference Between Coronavirus and SARS

Difference Between Coronavirus and Influenza

Difference Between Coronavirus and Influenza

Difference Between Coronavirus and Covid 19

Difference Between Coronavirus and Covid 19

You May Like

Difference Between Accusative and Dative

Difference Between Accusative and Dative

Difference Between Schizophrenia and Alzheimer’s

Difference Between Schizophrenia and Alzheimer’s

Difference Between Entrepreneur and Trader

Difference Between Webinar and Webcast

Difference Between Webinar and Webcast

Difference Between Dragonfly and Damselfly

Latest Posts

  • 的区别是什么Between Aquagenic Urticaria and Aquagenic Pruritus
  • 的区别是什么Between Astringent and Toner
  • 的区别是什么Between Esophagitis and Barrett’s Esophagus
  • 的区别是什么Between Alcohol Ink and Resin Dye
  • 的区别是什么Between Hyperparathyroidism and Hyperthyroidism
  • 的区别是什么Between Pearlescent and Iridescent
  • Home
  • Vacancies
  • About
  • Request Article
  • Contact Us

Copyright © 2010-2018Difference Between. All rights reserved.Terms of Useand Privacy Policy:Legal.