CS1S467 GUI Programming LECTURE 14 Methods (1 of 2)

Slides:



Advertisements
Similar presentations
Programming Methodology (1). Implementing Methods main.
Advertisements

Spring Semester 2013 Lecture 5
Procedural programming in Java
Computer and Programming
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 10: 1 TEST!!
Advanced Programming LOOP.
C# B 1 CSC 298 Writing a C# application. C# B 2 A first C# application // Display Hello, world on the screen public class HelloWorld { public static void.
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
Introduction to Java Thanks to Dan Lunney (SHS). Java Basics File names The “main” method Output to screen Escape Sequence – Special Characters format()
110-G1 Motivation: Within a program, may have to perform the same computation over and over Many programs share the same computation (e.g. sorting) To.
NA2204.1jcmt CSE 1320 Intermediate Programming C Program Basics Structure of a program and a function type name (parameters) { /* declarations */ statement;
Chapter 4: Subprograms Functions for Problem Solving Mr. Dave Clausen La Cañada High School.
Methods We write methods in our programs for many reasons:
User Defined Methods Methods are used to divide complicated programs into manageable pieces. There are predefined methods (methods that are already provided.
C# Programming Methods.
Copyright © Curt Hill Simple I/O Input and Output using the System and Scanner Objects.
Lecture 2 Review of 1301 and C# Richard Gesick. Common data types int, long, double, float, char, bool, string.
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
CSC 1010 Programming for All Lecture 5 Functions Some material based on material from Marty Stepp, Instructor, University of Washington.
Java 5 Class Anatomy. User Defined Classes To this point we’ve been using classes that have been defined in the Java standard class library. Creating.
Comp1004: Building Better Objects I Methods. Coming up Methods and Parameters – Why Parameterise? – Call by value, call by reference Return Types – Methods.
C# Part 1 Intro to C#. Background Designed to be simple, modern, general- purpose, OO, programming language Strong type checking, array bounds checking,
Information and Computer Sciences University of Hawaii, Manoa
Functions + Overloading + Scope
Programming what is C++
User-Written Functions
Concepts of Object Oriented Programming
Chapter 7 User-Defined Methods.
Chapter 6: User-Defined Functions I
COMP 170 – Introduction to Object Oriented Programming
USING ECLIPSE TO CREATE HELLO WORLD
UNIT - V STORED PROCEDURE.
Lecture Note Set 1 Thursday 12-May-05
Introduction to Methods in java
User-Defined Functions
Lecture 11 B Methods and Data Passing
CS1S467 GUI Programming LECTURE 15 Methods (2 of 2)
CSC 253 Lecture 8.
2008/11/05: Lecture 15 CMSC 104, Section 0101 John Y. Park
Subroutines Idea: useful code can be saved and re-used, with different data values Example: Our function to find the largest element of an array might.
Functions Used to write code only once Can use parameters.
Lecture 11 C Parameters Richard Gesick.
Lecture 3 Functions Simple functions
CS1S467 GUI Programming Lecture 3 Variables 2.
CSC 253 Lecture 8.
Chapter 6 Methods: A Deeper Look
Group Status Project Status.
Functions, Part 1 of 3 Topics Using Predefined Functions
CISC101 Reminders Assn 3 due tomorrow, 7pm.
Methods and Data Passing
Take out a piece of paper and PEN.
Language Constructs Construct means to build or put together. Language constructs refers to those parts which make up a high level programming language.
Lecture 1 Review of 1301/1321 CSE /26/2018.
Scope of variables class scopeofvars {
CSE Module 1 A Programming Primer
Functions, Part 1 of 3 Topics Using Predefined Functions
SE1H421 Procedural Programming LECTURE 3 Variables (2)
ENERGY 211 / CME 211 Lecture 8 October 8, 2008.
Functions, Part 1 of 3 Topics Using Predefined Functions
2008/11/05: Lecture 15 CMSC 104, Section 0101 John Y. Park
CISC101 Reminders Assignment 3 due today.
CSE Module 1 A Programming Primer
Methods (a.k.a functions)
CPS125.
Lecture 1 Review of 1301/1321 CSE /26/2018.
Methods and Data Passing
Presentation transcript:

CS1S467 GUI Programming LECTURE 14 Methods (1 of 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

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

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….

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

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

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

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

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?

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

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'

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

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

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" );

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.

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

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

END OF LECTURE 14