DOC PREVIEW
CUW CSC 250 - Methods

This preview shows page 1-2-19-20 out of 20 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 20 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 20 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 20 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 20 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 20 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Methods 2.1. Review.Review (cont.)Slide 4Value vs. address.Some general rules.2. Parameter passing mechanisms.A. No parameters.B. Data is sent only.C. Data is returned only.Slide 11D. Data is sent and returned.Ref parameters.3. Choosing the right methods.Return parameters.Out, out, parameters, out I say. In thy closet hast thee dwelt too long….It’s a fair cop, but the ref’s to blame.4. Adding methods.Adding methods (cont.).Slide 20Methods 2.1. Review.2. Parameter passing mechanisms.3. Choosing the right mechanism.4. Adding methods.1. Review.A. For user-defined methods in C#, we need:(1) a method declaration—where?(2) a method call---where?B. What is meant by “transfer of control”?How is it illustrated by methods?Review (cont.)Modules (such as main and another method) communicate via parameters.The parameters in the method call are called ACTUAL parameters.The parameters in the method heading are called FORMAL parameters.A method call can send parameters to a method and receive results from the method.Review (cont.)There are 2 types of formal parameter. (1)A value parameter receives a copy of the actual parameter, but the copy has its own address. Therefore changing the copy does not change the original. See Methods_2.cs.(2) A reference / address parameter receives the address of the actual parameter. So changing an address parameter does change the original. See Methods_3.cs.Value vs. address.(1) Value parameters can be variables, literals (e.g. 4.75, “Hello,” true, or expressions, such as arithmetic / logic expressions or even method calls e.g. Answer = Convert.ToInt32 (Console.ReadLine ()); // the value returned from Console.ReadLine() is sent to the method Convert.ToInt32.(2) Address parameters MUST be variables. Why?Some general rules.(1) Value parameters are safer (the original data cannot be corrupted).(2) Value parameters do have an overhead, since the data is copied, so…(3) Some data items cannot be passed by value (e.g. arrays, strings, structs and objects).(4) Reference must be used if you need to change the original e.g. for a Total.2. Parameter passing mechanisms.There are several possibilities when communicating with a method:A) None (and that’s final!).B) Data is only sent to the method.C) Data is only returned from the method.D) Data is sent to and returned from the method.A. No parameters.The ultimate in taciturn, non-communicative (Scottish?) methods has no arguments and a void return type.Nothing gets to this guy and you can’t get anything out of him; he just does his thing.See Methods_1.cs.Why have methods like this?B. Data is sent only.If data is sent only, then either it is not changed at all or, if it is, it is changed in the method, but the new value is not required afterwards.In this case, it makes sense to use a void method with value parameters.Why have methods like this?C. Data is returned only.If a single value is to be returned, it is possible to return it via the return statement.This requires us to code a return type for the method.See Methods_2.cs.Why have methods like this?What is the limitation?C. Data is returned only.If multiple items need to be returned, it is possible to code “out” parameters. These are address parameters, so changing them in the method also changes the actual parameters in the method call.See Methods_4.cs.“out” parameters must be changed in the closet, er, I mean, method.D. Data is sent and returned.If a single value is to be returned, one can use a combination of value parameters and a return type.If multiple values need to be returned, but they do not need an initial value, one can use out parameters.If they do need to be initialized, a ref parameter should be used.See Methods_3.cs.Ref parameters.A ref parameter must be initialized before being included as an actual parameter in a method call.The ref parameter may simply be used in the method (but if so a value parameter would be safer).Or it can be updated so that processing after the method uses the new value.3. Choosing the right methods.When, in general, should we use value parameters?1) For output methods—even if the values are modified in the method, they aren’t needed later.2) As input to calculations, e.g. if we want to calculate the volume of a cylinder, the radius and length could be value parameters, since they are input to the calculation, and won’t be changed.3) Any time the original value should be protected (marked “read only”).Return parameters.When, in general, should we use return parameters?1) Whenever we are implementing a strict mathematical function (1 to 1 like sqrt) or many 1 (like average).2) When there are many possible values to return, but only 1 will be returned on any given use of the program, e.g. an error code (1, 2, 3, 4….), or a boolean function (true, false).Out, out, parameters, out I say. In thy closet hast thee dwelt too long….When, in general, should we use out parameters?1) When returning multiple values for a calculation e.g. the compound interest and future value of an investment, or “yes/no” a value was found in an array, and its location in the array.2) When we do not need to (or know how to) assign an initial value to an actual parameter before calling a method, but we do need the method to assign a value.It’s a fair cop, but the ref’s to blame.When, in general, should we use ref parameters?1) When we wish to send a variable with an initial value and determine its final value, e.g. send a Total initialized to zero, and calculate the final value, or send the starting location for a search, then update the Location to where the item is found or passed.2) Whenever subsequent processing depends on an updated version of the original value.4. Adding methods.A helpful way to begin is to think about what changes are needed to go from a program with no methods to a program with them.To design methods, we first need to think carefully about the interface (what needs to be sent to and returned from the method). Then we need to design the implementation.Adding methods (cont.).See the example Without_User_Method.cs which calculates the interest per period, total times compounded and future value of an investment. How would we transform this into a version that has a reusable user-defined


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?