(original) C # learning note 06--function 02--variable scope 02--parameter and return value with global data

Source: Internet
Author: User

6.2.2 parameters and return values with global data

This section details how to exchange data with functions through global data and parameters and return values. First look at the following code:

classProgram {Static voidShowdouble (ref intval) {Val*=2; Console.WriteLine ("val doubled = {0}", Val); }     Static voidMain (string[] args) {         intval =5; Console.WriteLine ("val = {0}", Val); Showdouble (refval); Console.WriteLine ("val = {0}", Val); } } 

Compare with the following code:

classProgram {Static intVal; Static voidshowdouble () {Val*=2; Console.WriteLine ("val doubled = {0}", Val); }     Static voidMain (string[] args) {Val=5; Console.WriteLine ("val = {0}", Val);         Showdouble (); Console.WriteLine ("val = {0}", Val); } }

The results of these two showdouble () functions are the same.

First, in the first discussion of this issue, the showdouble () version using global values only uses the global variable val. In order to use this version, it is necessary to use this global variable. This can have a slight limitation on the diversity of the function, and if you want to store the result, you must always copy the global variable value into other variables. In addition, the global data can be modified by the code elsewhere in the application, which results in unexpected outcomes (whose values may change, which is too late when we realize this).

However, the loss of diversity is often beneficial. We often want a function to be used only for one purpose, using global data storage to reduce the likelihood of making a mistake in a function call, such as passing it to the wrong variable.

Of course, it can also be said that this simplification actually makes the code more difficult to understand. Explicitly specifying parameters can see what has changed at a glance. such as functionname (Val1, out val2) function calls, where val1 and val2 are important variables to consider, at the end of the function execution, VAL2 is fully assigned a new value. Conversely, if the function does not have parameters, it cannot manipulate what data it has.

In short, you have the freedom to choose which technology to use to exchange data. In general, it is best to use parameters rather than global data, but sometimes it is more appropriate to use global data, and there is no mistake in using this technique.

(original) C # learning note 06--function 02--variable scope 02--parameter and return value with global data

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.