Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS1S467 GUI Programming LECTURE 14 Methods (1 of 2)

Similar presentations


Presentation on theme: "CS1S467 GUI Programming LECTURE 14 Methods (1 of 2)"— Presentation transcript:

1 CS1S467 GUI Programming LECTURE 14 Methods (1 of 2)

2 Why METHODS ? You call METHODS What actually happens when you write…
... this in C# ? Console.WriteLine("Hello World"); aString = Console.ReadLine(); MessageBox.Show("Good Bye, World"); You call METHODS

3 Why METHODS ? METHODS are used whenever:
a problem can be broken down into sub-problems similar or identical code is required at several places in the program Consequences of using METHODS: less typing re-using of code produces better quality and easier to maintain programs

4 What's in a Name ? METHODS are are also called:
Functions (C, C++, FORTRAN, …) Procedures (Pascal, Delphi, …) Sub-programs or subs (BASIC, VB, …) No matter what they are called, they always have the following structure in common….

5 Structure of a METHOD To call a method: The method itself:
Returned stuff = Method name ( List of stuff to pass into method ); The method itself: Scope Returned stuff Method name (List of stuff passed in) body of the method where computations are made return statement (if anything gets returned) { } // end of method

6 We have already used METHODS
To call a method: To call 'Console.ReadLine( )': myString Console.ReadLine ( ); Returned stuff = Method name ( List of stuff to pass into method ) ; The method itself: The method 'ReadLine' in the 'Console' class: Scope Returned stuff Method name (List of stuff passed in) body of the method where computations are made return statement (if anything gets returned) ( ) static String ReadLine { Terribly complicated code here, filling a String called 'theInput' with letters typed in from the keyboard return theInput } // end of method

7 Or what about this one ? To call a method: The method itself:
To call 'Console.WriteLine': Console.WriteLine ("Hello world") ; Returned stuff = Method name (List of stuff to pass into method ) ; The method itself: The method 'WriteLine' in the 'Console' class: ( String textToPrint ) static void WriteLine Scope Returned stuff Method name List of stuff passed in body of the method where computations are made return statement (if anything gets returned) { Terribly complicated code here, accessing the console and printing the text held in textToPrint on screen // nothing to return } // end of method

8 And what about this one? The method itself: The method 'Main' :
Who calls 'Main’ in C# ? Program.cs Why ( string[ ] args) ? user can provide an array of parameters to main The method itself: The method 'Main' : Scope Returned stuff Method name (List of stuff passed in) body of the method where computations are made return statement (if anything gets returned) ( string[ ] argStr ) static void Main { Terribly complicated code here, written by a genius and unfairly marked by a moron lecturer….. // nothing to return } // end of method

9 Example Would a construction such as...
CurrencyOuput("The value is: ", value); …be a bit more convenient than this… pounds = (int) (value / 100.0); pence = (int) value % 100; Console.WriteLine(" The value is: " +pounds+ " pounds " +pence+ " pence"); …to produce exactly the same output?

10 Implementation of 'currencyOutput'
To call a method: To call 'currencyOutput': CurrencyOutput ("The value is: ", value); Returned stuff = Method name ( List of stuff to pass into method ) ; The method itself: The method 'currencyOutput' in the current class: Scope Returned stuff Method name (List of stuff passed in) body of the method where computations are made return statement (if anything gets returned) { (String txt, float val ) static void CurrencyOutput { int pounds, pence; pounds = (int) (val / 100.0); pence = (int) val % 100; Console.WriteLine(txt + pounds+" pounds " +pence+" pence"); // nothing to return } // end of method

11 Task Now Write a method called 'MaxOf' that returns the largest of two int values and that is called like this: class Test { // the method // will be implemented // here, above the // main method static void Main ( string [ ] argStr ) { int a=4, b=5, c; c = MaxOf ( a, b ); // calling the 'MaxOf' method Console.WriteLine("The largest value of " +a+ " and " +b+ " is " +c); } // end of Main } // end of class static int MaxOf( int x, int y ) { if ( x>y ) return x; else return y; } // end of method 'MaxOf'

12 The formal parameter list The actual parameter list
Parameter Lists The formal parameter list static int MaxOf( int x, int y ) { if ( x>y ) return x; else return y; } // end of method 'MaxOf' The 'MaxOf' method may be called using 'actual parameter lists' variables as parameters: MaxOf( a, b ); // a and b are int variables actual figures as parameters: MaxOf( 7, 100 ); // 7 and 100 also int or a mixture of both: MaxOf( a, 100 ); // a and 100 are int The actual parameter list

13 Parameter Lists (cont)
The formal parameter list Formal and Actual parameter lists must match static int MaxOf( int x, int y ) { if ( x>y ) return x; else return y; } // end of method 'MaxOf' The 'MaxOf' method may NOT be called using unsuitable parameters types: MaxOf( a, 7.5 ); // 7.5 is a float the wrong amount of parameters: MaxOf( 7, 100, 200 ); // 3 parameters MaxOf( 100 ); // only 1 parameter

14 Methods and Classes To call a method in the same class use just the name of the method MaxOf(4, 5); If the method is located in another class call it using the format: ClassName.MethodName( formal parameter list ) Console.ReadLine(); Console.WriteLine( "bla" );

15 Check your knowledge now:
Spot the mistakes in the code: class testIt { int PromptAndRead( char c, string txt) { int x; Console.WriteLine( txt + c ); Console.ReadLine(); return x; } // end of method 'promptAndRead' static void Main ( string [ ] argStr ) { float in; in = PromptAndRead( Enter a value , ':' ); Console.WriteLine("You entered: " +in); } // end of Main } // end of class class name: capital 'T' static int…. x=int.Parse( Console.ReadLine() ); alternative: x = Convert.ToInt32(Console.Readline()); 'in' has to be an int text not in " ", also wrong order of param.

16 Summary So Far The structure of a method call is:
Returned parameter = Class.Method name (Actual parameter list) ; Single parameter, must match in type (void if none) Parameters must match in: number type order The structure of a method is: Scope Returned type Method name (Formal parameter list) body of the method where computations are made return statement (if anything gets returned) { } // end of method

17 Summary so far: METHODS
Why: maintenance, usability, readability, safety Parameter list: formal - actual, match (amount, order, type) Return types: void, int, float, String, …. Method scope: private, public, (friendly) Variable scope: class variables, method variables --> "safety" Accessing methods: ClassName.methodName Overloading methods: same name, different parameter lists

18 END OF LECTURE 14


Download ppt "CS1S467 GUI Programming LECTURE 14 Methods (1 of 2)"

Similar presentations


Ads by Google