ChapterFivePartI

ChapterFivePartI - IT108 Introduction to Methods Part I...

Info iconThis preview shows pages 1–14. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: IT108 Introduction to Methods Part I George Mason University Irene Bruno, with modifications by Bill Pegram Functions/Methods Program methods/Functions sub-programs Allow for breaking the task into parts Also allow for reusing code Definition of method vs. call of method Method A method - series of statements that carry out a task Any class can contain an unlimited number of methods Methods Method declarations must include: A header An opening curly brace A body A closing brace Method Header Access: public/private/protected Return type Void (no value returned), or Datatype of value being returned Method Name Follows variable naming rules Parentheses Parameters Datatype and parameter names Access Modifiers Access modifiers for a method can be: public- most often methods are given public access, Endowing a method with public access means any class can use it Private discussed in later chapter Protected discussed in later chapter Method Definition Body enclosed in { } One or more statements within body Immediately follows the header Return Must be included for non-void methods Lives inside class but not within any other method Methods with Parameters(s) The method declaration must include: The type of each formal parameter The name of each formal parameter For example: public void predictRaise(double moneyAmount) Creating Methods with Parameters The parameter list for a method can have more than one parameter, they are separated by commas The declaration for a method that receives two or more arguments must list the type for each parameter separately Methods Argument(s) Arguments- Are communications to a method Implementation hiding- Allows that the invoking program must know the name of the method and what type of information to send it, but the program does not need to know how the method works Pass multiple arguments by: Listing the arguments in the call to the method Separating them with commas Calling a Method with Arguments Argument can be: Constant e.g. predictRaise(472.55) Variable e.g. predictRaise(mySalary) Expression Function call (with a return value) Method Call Invoked from other method when the action is appropriate There are no datatypes if method call, even if method is called with arguments that are variables; the datatypes are only shown in the method definition Call to a non-void method Lives within an expression average = calculateAverage(43, 49); Call to a void method Lives in a statement by itself displayAverage(average); Methods Method signature combination of method name and parameter list variables defined in method header -- formal parameters ....
View Full Document

Page1 / 39

ChapterFivePartI - IT108 Introduction to Methods Part I...

This preview shows document pages 1 - 14. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online