Unformatted text preview:

Methods 3.1. Review.Review.Review (cont.).2. Class-wide vs. local variables.What are the local variables? Local to what?“Local” is a relative term.Why local variables?Uses of local variables.Class-wide variables.Slide 11Note the differences:3. Why C# bans global variables.A Global variable in C++Global variables.What’s wrong with global variables?Why bother with parameters?What are side-effectsSide effects (cont.).Solution of C#4. Nested blocks.Nesting (cont.).Slide 23Nesting and local variables.Nested blocks (cont.).Slide 26What if we do use the same name for a class-wide and a local variable?Example:5. Scope of identifiers.Scope of identifiers (cont.).Slide 31Slide 32Slide 33Methods 3.1. Review.2. Class-wide vs. local variables.3. Why C# bans global variables.4. Nested blocks.5. Scope of identifiers.1. Review.Recall that creating a function requires both (a) designing the interface = the visible, communication mechanism and (b) designing the implementation = the “invisible” code hidden inside the function.A. The interface = the WHAT; includes function name (the task) and the parameters (communication). It specifies what is to be done and what gets sent back and forth.Review.Communication requires matching the actual parameters (in the function call) with the formal parameters (in the function heading). How are parameters matched?NOT by name—the name can be the same or different.By: 1) data type; 2) number; 3) position.Review (cont.).B. The implementation = the HOW; behind the scenes, what machinery (code) actually carries out the task. This includes all the code inside the function’s scope brackets { }.The implementation may include “local variables”….2. Class-wide vs. local variables.Definition.A local variable is defined inside a block of code, i.e. within scope brackets { }, and is only accessible there.Memory for local variables is allocated in a region called the stack on a demand basis: it does not exist before the block is entered or after the block is exited.What are the local variables? Local to what?class Bundle {static double Find_Big (double X, double Y) {  double Big;  Big = X; if (Y > X) Big = Y; return Big; } // end Find_Bigstatic void Main (string[] arg)  { double Num1, Num2, Answer; Answer = Find_Big (Num1, Num2); } // end Main()} // end Bundle“Local” is a relative term.Local to Find_Big: 1) the formal parameters X and Y. 2) BigLocal to Main(): Num1, Num2, AnswerFind_Big CANNOT access Num1, Num2, Answer. Why?Main() CANNOT access X, Y or Big. Why?Why local variables?We typically have variables local to Main(), but we may also put local variables inside our own Methods (or in other blocks of code). WHY? What are they for?Uses of local variables.Local variables are a good solution for indexes and intermediary values that are only needed within a method (or other block).They are not a good solution if the data values are required after the method / block is exited, because?Class-wide variables.If data needs to survive after a method call, or it makes sense for several methods to share data, C# allows class-wide variables.These are data members. All methods in the class can access these variables directly without passing them as parameters. They are “common knowledge,” to everyone in the class.Class-wide variables.class Bundle { double Big; // Big is a class-wide variable.static void Find_Big (double X, double Y) { Big = X; if (Y > X) Big = Y;} // end Find_Bigstatic void Main (string[] arg) { double Num1, Num2; Find_Big (Num1, Num2); Console.Write (“The answer is ” + Big); // legal!} // end Main()} // end BundleNote the differences:Both Find_Big and Main() can access the class-wide variable Big directly, without passing it as a parameter.Does that mean anyone can access Big?No. Only class members can see Big.Can a local variable have the same name as a class-wide variable?Yes, but it is not a good idea. More later.3. Why C# bans global variables.Some languages allow global variables, e.g. C and C++.Global variables are defined outside of any class at “file scope.”They can therefore be accessed by methods in any class.Using global variables, on can avoid parameter passing altogether.Here is an example in C++:A Global variable in C++#include <iostream.h>int Global_Num; // a global variable.int main () { Global_Num = 5; // accesses global variable}void Another_Function () { Global_Num = 7; // does it again}Global variables.Notice that if a variable is defined globally, it can be accessed anywhere, *without* passing it as a parameter.But then why pass parameters? If we don’t, too much danger of data corruption. WHY?What’s wrong with global variables?Global variables lead to poor interface design. They make it unclear to another programmer what is supposed to pass in and out of a function. Listing parameters forces us to think precisely about the communication between functions.Why bother with parameters?Using parameters is like “going through the proper channels. It avoids doing an end-run / going around the side.If we don’t do this, we allow “side effects.”What are side-effectsDefinition: A side effect is any communication between 2 modules (functions) that is not specified by the interface.Side effects (cont.).Analogies:1. Restaurant analogy. See diagram. Suppose the cook brings out food with no input from the customer, or the customer goes straight into the kitchen for food!2. Theology: claiming Jesus is one of many ways to access the Father, yet there is only *one* mediator between God and man (1 Tim. 2: 5).Solution of C#C# is a more constrained language than C++.In addition to rigorous type-checking, checking for initialization of variables and code-path checking, the designers of C# chose to remove temptation:C# bans global variables.4. Nested blocks.Consider Russian dolls:a miniature doll fits inside a slightly larger duplicate which fits inside another, until we reach the outermost doll. The smaller dolls are nested inside the larger dolls.Nesting (cont.).Likewise, in a computer program, one block can be nested inside another.static void Main () { //outer blockwhile (Count


View Full Document

CUW CSC 250 - Methods

Download Methods
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Methods and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Methods 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?