Follow the rules for creating an identifier pascal

This preview shows page 13 - 25 out of 72 pages.

Follow the rules for creating an identifier Pascal case style Action verb or prepositional phrase Examples CalculateSalesTax( ) AssignSectionNumber( ) DisplayResults( ) InputAge( ) ConvertInputValue( )
Image of page 13

Subscribe to view the full document.

C# Programming: From Problem Analysis to Program Design 14 Parameters Supply unique data to method Appear inside parentheses Include data type and an identifier In method body, reference values using identifier name Parameter refers to items appearing in the heading Argument for items appearing in the call Formal parameters Actual arguments
Image of page 14
C# Programming: From Problem Analysis to Program Design 15 Parameters ( continued ) public static double CalculateMilesPerGallon ( int milesTraveled, double gallonsUsed) { return milesTraveled / gallonsUsed; } Call to method inside Main( ) method Console.WriteLine("Miles per gallon = {0:N2}", CalculateMilesPerGallon (289, 12.2)); Two formal parameters Actual arguments
Image of page 15

Subscribe to view the full document.

C# Programming: From Problem Analysis to Program Design 16 Parameters ( continued ) Like return types, parameters are optional Keyword void not required (inside parentheses) – when there are no parameters public void DisplayMessage( ) { Console.Write("This is "); Console.Write("an example of a method"); Console.WriteLine("body."); return ; // no value is returned }
Image of page 16
C# Programming: From Problem Analysis to Program Design 17 Method Body Enclosed in curly braces Include statements ending in semicolons Declare variables Do arithmetic Call other methods Value-returning methods must include return statement
Image of page 17

Subscribe to view the full document.

C# Programming: From Problem Analysis to Program Design 18 Calling Class Methods Also called invoking a method Call to method that returns no value [qualifier].MethodName(argumentList); Qualifier Square brackets indicate optional Class or object name Call to method does not include data type Use IntelliSense
Image of page 18
C# Programming: From Problem Analysis to Program Design 19 IntelliSense Method signature(s) and description After typing the dot, list of members pops up 3-D box — member methods Figure 3-2 Console class members
Image of page 19

Subscribe to view the full document.

C# Programming: From Problem Analysis to Program Design 20 Predefined Methods C# has extensive class library Classes have number of predefined methods Console class (in System namespace ) Overloaded methods Multiple methods with same name, but each has different number or type of parameter
Image of page 20
C# Programming: From Problem Analysis to Program Design 21 IntelliSense Display string argument expected string parameter 18 different Write( ) methods Figure 3-3 IntelliSense display
Image of page 21

Subscribe to view the full document.

C# Programming: From Problem Analysis to Program Design 22 Predefined Methods Console class Overloaded methods Write( ) WriteLine( ) Read( ) Not overloaded Returns an integer
Image of page 22
C# Programming: From Problem Analysis to Program Design 23 Read( ) Method Figure 3-4 Console.Read ( ) signature
Image of page 23

Subscribe to view the full document.

C# Programming: From Problem Analysis to Program Design 24 Call Read( ) Methods int aNumber; Console.Write("Enter a single character: "); aNumber = Console.Read( ); Console.WriteLine("The value of the character entered: " + aNumber); Enter a single character: a The value of the character entered: 97
Image of page 24
Image of page 25
  • Fall '16
  • Elise Wade
  • Program design, Subroutine, Doyle, CarpetExampleWithClassMethods

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern