1 / 13

Using Methods

Using Methods. Problem. How to create a program that can convert temperature?. Methods. A method is a member that implements a computation or action that can be performed by an object or class . (C# Language Specification, p. 15) Used for a small block of code that may be resused.

duke
Télécharger la présentation

Using Methods

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Using Methods

  2. Problem • How to create a program that can convert temperature?

  3. Methods • A method is a member that implements a computation or action that can be performed by an object or class. (C# Language Specification, p. 15) • Used for a small block of code that may be resused

  4. Possible Methods • Convert Celsius to Fahrenheit • Convert Fahrenheit to Celsius

  5. Pseudocode • Main()Prompt the user for the conversion type • fahrenheitToCelsius()Prompt the user for a temperature in degrees FahrenheitConvert the temperature to degrees CelsisusDisplay the temperature • celsiusToFahrenheit()Prompt the user for a temperature in degrees CelsiusConvert the temperature to degrees FahrenheitDisplay the temperature

  6. Fahrenheit to Celsius method

  7. Celsius to Fahrenheit method

  8. Main Method

  9. Creating a method • public: method-modifier • Static: method-modifier • Void: return-type • celsiusToFahrenheit(): method-name • The body of the method follows in a block of code defined by {//code goes here}

  10. Parameters

  11. Return

  12. Scope • A variable declared within a method is no longer available after the method has run (Local Scope) • The static keyword allows this variable to be used in any method in the class Program

  13. Assessment of Learning • March 8 – Test • March 8 – Programming assignment

More Related