SlideShare a Scribd company logo
1 of 8
Download to read offline
Methods with parameters
Monday, September 9, 13
2
Parameters
• The method below will print the first 5 squares:
public class ParameterTest {
public static void printSquares() {
for (int i = 1; i <= 5; i++) {
System.out.println(i + " squared = " + i * i);
}
}
public static void main(String[] args) {
printSquares();
}
}
• What if we wanted to make this print an arbitrary number of
squares?
Monday, September 9, 13
3
Parameters
• You could assign try to assign a variable to do it:
public class ParameterTest {
public static void printSquares() {
for (int i = 1; i <= maxSquare; i++) {
System.out.println(i + " squared = " + i * i);
}
}
public static void main(String[] args) {
int maxSquare = 5;
printSquares();
}
}
// won’t work!
Monday, September 9, 13
4
Parameters
• Instead, we can have printSquares() take a parameter:
public class ParameterTest {
// maxSquare is a variable local to the method
public static void printSquares(int maxSquare) {
for (int i = 1; i <= maxSquare; i++) {
System.out.println(i + " squared = " + i * i);
}
}
public static void main(String[] args) {
printSquares(5);
}
}
• formal parameter (variable) vs. actual parameter (value)
Monday, September 9, 13
5
Parameters
• The actual parameters passed to a method call can be
expressions:
public class ParameterTest {
public static void printSquares(int maxSquare) {
for (int i = 1; i <= maxSquare; i++) {
System.out.println(i + " squared = " + i * i);
}
}
public static void main(String[] args) {
int firstMax = 5, secondMax = 8;
printSquares(firstMax); // prints all squares to 5
printSquares(secondMax + 1); // prints all squares to 9
}
}
Monday, September 9, 13
6
Scoping methods
public class ParameterTest {
public static void printSquares(int maxSquare) {
for (int i = 1; i <= maxSquare; i++) {
System.out.println(i + " squared = " + i * i);
}
}
public static void main(String[] args) {
int firstMax = 5, secondMax = 8;
printSquares(firstMax); // prints all squares to 5
printSquares(secondMax + 1); // prints all squares to 9
}
}
method main method printSquares method printSquares
firstMax
secondMax
maxSquare maxSquare5
8
5 9
Monday, September 9, 13
7
Scoping methods
• Be careful with the way you name parameters:
public class ParameterExample {
public static void main(String[] args) {
int x = 4;
doubleNumber(x);
System.out.println("x = " + x);
}
public static void doubleNumber(int x) {
System.out.println("Initial value = " + x);
x *= 2;
System.out.println("Final value = " + x);
}
}
method main method doubleNumber
x x4 48
Monday, September 9, 13
8
Lab!
• See https://dl.dropboxusercontent.com/u/20418505/Labs/
M1.W4-1.txt
Monday, September 9, 13

More Related Content

What's hot

Python decision making_loops_control statements part9
Python decision making_loops_control statements part9Python decision making_loops_control statements part9
Python decision making_loops_control statements part9Vishal Dutt
 
IGraph a tool to analyze your network
IGraph a tool to analyze your networkIGraph a tool to analyze your network
IGraph a tool to analyze your networkPushpendra Tiwari
 
Basic Traversal and Search Techniques
Basic Traversal and Search TechniquesBasic Traversal and Search Techniques
Basic Traversal and Search TechniquesSVijaylakshmi
 
Basic Traversal and Search Techniques
Basic Traversal and Search TechniquesBasic Traversal and Search Techniques
Basic Traversal and Search TechniquesSVijaylakshmi
 
Introduction to programming class 11 exercises
Introduction to programming   class 11 exercisesIntroduction to programming   class 11 exercises
Introduction to programming class 11 exercisesPaul Brebner
 
Public class arithmetic operatordemo
Public class arithmetic operatordemoPublic class arithmetic operatordemo
Public class arithmetic operatordemoCliff Rodrigo
 
Javascript Array map method
Javascript Array map methodJavascript Array map method
Javascript Array map methodtanerochris
 
substring & subSquence & find problem solving
substring & subSquence & find problem solving substring & subSquence & find problem solving
substring & subSquence & find problem solving hussein zayed
 
Chapter 7.4
Chapter 7.4Chapter 7.4
Chapter 7.4sotlsoc
 
Mat lab lecture part 1
Mat lab lecture part 1Mat lab lecture part 1
Mat lab lecture part 1OmGulshan
 
Parent Functions
Parent FunctionsParent Functions
Parent FunctionsVLB10525
 
queue & its applications
queue & its applicationsqueue & its applications
queue & its applicationssomendra kumar
 
functions
functionsfunctions
functionssmirn4
 
Lecture 3.1 to 3.2 bt
Lecture 3.1 to 3.2 btLecture 3.1 to 3.2 bt
Lecture 3.1 to 3.2 btbtmathematics
 

What's hot (19)

Python decision making_loops_control statements part9
Python decision making_loops_control statements part9Python decision making_loops_control statements part9
Python decision making_loops_control statements part9
 
Lecture 3.6 bt
Lecture 3.6 btLecture 3.6 bt
Lecture 3.6 bt
 
IGraph a tool to analyze your network
IGraph a tool to analyze your networkIGraph a tool to analyze your network
IGraph a tool to analyze your network
 
Basic Traversal and Search Techniques
Basic Traversal and Search TechniquesBasic Traversal and Search Techniques
Basic Traversal and Search Techniques
 
Basic Traversal and Search Techniques
Basic Traversal and Search TechniquesBasic Traversal and Search Techniques
Basic Traversal and Search Techniques
 
Introduction to programming class 11 exercises
Introduction to programming   class 11 exercisesIntroduction to programming   class 11 exercises
Introduction to programming class 11 exercises
 
Public class arithmetic operatordemo
Public class arithmetic operatordemoPublic class arithmetic operatordemo
Public class arithmetic operatordemo
 
Javascript Array map method
Javascript Array map methodJavascript Array map method
Javascript Array map method
 
substring & subSquence & find problem solving
substring & subSquence & find problem solving substring & subSquence & find problem solving
substring & subSquence & find problem solving
 
Chapter 7.4
Chapter 7.4Chapter 7.4
Chapter 7.4
 
Mat lab lecture part 1
Mat lab lecture part 1Mat lab lecture part 1
Mat lab lecture part 1
 
Parent Functions
Parent FunctionsParent Functions
Parent Functions
 
Parent Function Project
Parent Function ProjectParent Function Project
Parent Function Project
 
Queue
QueueQueue
Queue
 
queue & its applications
queue & its applicationsqueue & its applications
queue & its applications
 
C# 8 and Beyond
C# 8 and BeyondC# 8 and Beyond
C# 8 and Beyond
 
functions
functionsfunctions
functions
 
Graphing functions
Graphing functionsGraphing functions
Graphing functions
 
Lecture 3.1 to 3.2 bt
Lecture 3.1 to 3.2 btLecture 3.1 to 3.2 bt
Lecture 3.1 to 3.2 bt
 

Viewers also liked

Study of Performance and Compensation at Infosys Ltd.
Study of Performance and Compensation at Infosys Ltd.Study of Performance and Compensation at Infosys Ltd.
Study of Performance and Compensation at Infosys Ltd.Hitaishi Gupta
 
Compensation plan ppt
Compensation plan pptCompensation plan ppt
Compensation plan pptManav Badhwar
 
Hrm practices at telenor
Hrm practices at telenorHrm practices at telenor
Hrm practices at telenorKamran Arshad
 
IBM India - HR practices
IBM India - HR practicesIBM India - HR practices
IBM India - HR practicesnehajain248
 
HR practices in infosys Ltd
HR practices in infosys LtdHR practices in infosys Ltd
HR practices in infosys LtdLeesa Shah
 
Business Strategies adopted by Cafe Coffee Day
Business Strategies adopted by Cafe Coffee Day Business Strategies adopted by Cafe Coffee Day
Business Strategies adopted by Cafe Coffee Day Rohan Bharaj
 
Motivation and Compensation of Sales People
Motivation and Compensation of Sales PeopleMotivation and Compensation of Sales People
Motivation and Compensation of Sales PeopleKaushik Maitra
 
Hero motocorp ltd full PPT
Hero motocorp ltd full PPTHero motocorp ltd full PPT
Hero motocorp ltd full PPTSushant N'kkr
 
Cafe Coffee day (CCD)
Cafe Coffee day (CCD)Cafe Coffee day (CCD)
Cafe Coffee day (CCD)Sanjay Gupta
 
Maruti suzuki ppt
Maruti suzuki pptMaruti suzuki ppt
Maruti suzuki pptanurag77
 

Viewers also liked (20)

Ibm ppt
Ibm pptIbm ppt
Ibm ppt
 
Compensation imt1
Compensation imt1Compensation imt1
Compensation imt1
 
Study of Performance and Compensation at Infosys Ltd.
Study of Performance and Compensation at Infosys Ltd.Study of Performance and Compensation at Infosys Ltd.
Study of Performance and Compensation at Infosys Ltd.
 
Employee Compensation
Employee CompensationEmployee Compensation
Employee Compensation
 
Hr practices in ibm india
Hr practices in ibm indiaHr practices in ibm india
Hr practices in ibm india
 
Compensation plan ppt
Compensation plan pptCompensation plan ppt
Compensation plan ppt
 
Hrm practices at telenor
Hrm practices at telenorHrm practices at telenor
Hrm practices at telenor
 
Compensation practices
Compensation practicesCompensation practices
Compensation practices
 
IBM India - HR practices
IBM India - HR practicesIBM India - HR practices
IBM India - HR practices
 
HR practices in infosys Ltd
HR practices in infosys LtdHR practices in infosys Ltd
HR practices in infosys Ltd
 
Business Strategies adopted by Cafe Coffee Day
Business Strategies adopted by Cafe Coffee Day Business Strategies adopted by Cafe Coffee Day
Business Strategies adopted by Cafe Coffee Day
 
Ppt on tcs
Ppt on tcsPpt on tcs
Ppt on tcs
 
Motivation and Compensation of Sales People
Motivation and Compensation of Sales PeopleMotivation and Compensation of Sales People
Motivation and Compensation of Sales People
 
Hero motocorp ltd full PPT
Hero motocorp ltd full PPTHero motocorp ltd full PPT
Hero motocorp ltd full PPT
 
Presentation on Walmart
Presentation on WalmartPresentation on Walmart
Presentation on Walmart
 
Walmart ppt
Walmart pptWalmart ppt
Walmart ppt
 
Compensation & benefit presentation
Compensation & benefit presentation Compensation & benefit presentation
Compensation & benefit presentation
 
McKinsey presentation
McKinsey presentationMcKinsey presentation
McKinsey presentation
 
Cafe Coffee day (CCD)
Cafe Coffee day (CCD)Cafe Coffee day (CCD)
Cafe Coffee day (CCD)
 
Maruti suzuki ppt
Maruti suzuki pptMaruti suzuki ppt
Maruti suzuki ppt
 

Similar to m1.w4.d2 - parameters

JAVA PRACTICE QUESTIONS v1.4.pdf
JAVA PRACTICE QUESTIONS v1.4.pdfJAVA PRACTICE QUESTIONS v1.4.pdf
JAVA PRACTICE QUESTIONS v1.4.pdfRohitkumarYadav80
 
Driver.java import java.util.Scanner; import java.text.Decimal.pdf
Driver.java import java.util.Scanner; import java.text.Decimal.pdfDriver.java import java.util.Scanner; import java.text.Decimal.pdf
Driver.java import java.util.Scanner; import java.text.Decimal.pdfanandhomeneeds
 
ch03-parameters-objects.ppt
ch03-parameters-objects.pptch03-parameters-objects.ppt
ch03-parameters-objects.pptMahyuddin8
 
LECTURE 2 MORE TYPES, METHODS, CONDITIONALS.pdf
LECTURE 2 MORE TYPES, METHODS, CONDITIONALS.pdfLECTURE 2 MORE TYPES, METHODS, CONDITIONALS.pdf
LECTURE 2 MORE TYPES, METHODS, CONDITIONALS.pdfShashikantSathe3
 
Programming in Java: Organising Your Code
Programming in Java: Organising Your CodeProgramming in Java: Organising Your Code
Programming in Java: Organising Your CodeMartin Chapman
 
Methods Of Thread Class
Methods Of Thread ClassMethods Of Thread Class
Methods Of Thread Classkqibtiya5
 
Lab01.pptx
Lab01.pptxLab01.pptx
Lab01.pptxKimVeeL
 
Nested For Loops and Class Constants in Java
Nested For Loops and Class Constants in JavaNested For Loops and Class Constants in Java
Nested For Loops and Class Constants in JavaPokequesthero
 

Similar to m1.w4.d2 - parameters (20)

Java file
Java fileJava file
Java file
 
Java file
Java fileJava file
Java file
 
JAVA PRACTICE QUESTIONS v1.4.pdf
JAVA PRACTICE QUESTIONS v1.4.pdfJAVA PRACTICE QUESTIONS v1.4.pdf
JAVA PRACTICE QUESTIONS v1.4.pdf
 
Parameters
ParametersParameters
Parameters
 
14 thread
14 thread14 thread
14 thread
 
Driver.java import java.util.Scanner; import java.text.Decimal.pdf
Driver.java import java.util.Scanner; import java.text.Decimal.pdfDriver.java import java.util.Scanner; import java.text.Decimal.pdf
Driver.java import java.util.Scanner; import java.text.Decimal.pdf
 
ch03-parameters-objects.ppt
ch03-parameters-objects.pptch03-parameters-objects.ppt
ch03-parameters-objects.ppt
 
Ann
AnnAnn
Ann
 
STS4022 Exceptional_Handling
STS4022  Exceptional_HandlingSTS4022  Exceptional_Handling
STS4022 Exceptional_Handling
 
C# programs
C# programsC# programs
C# programs
 
39927902 c-labmanual
39927902 c-labmanual39927902 c-labmanual
39927902 c-labmanual
 
39927902 c-labmanual
39927902 c-labmanual39927902 c-labmanual
39927902 c-labmanual
 
LECTURE 2 MORE TYPES, METHODS, CONDITIONALS.pdf
LECTURE 2 MORE TYPES, METHODS, CONDITIONALS.pdfLECTURE 2 MORE TYPES, METHODS, CONDITIONALS.pdf
LECTURE 2 MORE TYPES, METHODS, CONDITIONALS.pdf
 
06slide.ppt
06slide.ppt06slide.ppt
06slide.ppt
 
Programming in Java: Organising Your Code
Programming in Java: Organising Your CodeProgramming in Java: Organising Your Code
Programming in Java: Organising Your Code
 
Methods Of Thread Class
Methods Of Thread ClassMethods Of Thread Class
Methods Of Thread Class
 
Lab01.pptx
Lab01.pptxLab01.pptx
Lab01.pptx
 
Java practical
Java practicalJava practical
Java practical
 
Nested For Loops and Class Constants in Java
Nested For Loops and Class Constants in JavaNested For Loops and Class Constants in Java
Nested For Loops and Class Constants in Java
 
Introduccion del curso
Introduccion del cursoIntroduccion del curso
Introduccion del curso
 

Recently uploaded

SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsKarinaGenton
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Micromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of PowdersMicromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of PowdersChitralekhaTherkar
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docxPoojaSen20
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting DataJhengPantaleon
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfUmakantAnnand
 

Recently uploaded (20)

SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its Characteristics
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Micromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of PowdersMicromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of Powders
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docx
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.Compdf
 

m1.w4.d2 - parameters

  • 2. 2 Parameters • The method below will print the first 5 squares: public class ParameterTest { public static void printSquares() { for (int i = 1; i <= 5; i++) { System.out.println(i + " squared = " + i * i); } } public static void main(String[] args) { printSquares(); } } • What if we wanted to make this print an arbitrary number of squares? Monday, September 9, 13
  • 3. 3 Parameters • You could assign try to assign a variable to do it: public class ParameterTest { public static void printSquares() { for (int i = 1; i <= maxSquare; i++) { System.out.println(i + " squared = " + i * i); } } public static void main(String[] args) { int maxSquare = 5; printSquares(); } } // won’t work! Monday, September 9, 13
  • 4. 4 Parameters • Instead, we can have printSquares() take a parameter: public class ParameterTest { // maxSquare is a variable local to the method public static void printSquares(int maxSquare) { for (int i = 1; i <= maxSquare; i++) { System.out.println(i + " squared = " + i * i); } } public static void main(String[] args) { printSquares(5); } } • formal parameter (variable) vs. actual parameter (value) Monday, September 9, 13
  • 5. 5 Parameters • The actual parameters passed to a method call can be expressions: public class ParameterTest { public static void printSquares(int maxSquare) { for (int i = 1; i <= maxSquare; i++) { System.out.println(i + " squared = " + i * i); } } public static void main(String[] args) { int firstMax = 5, secondMax = 8; printSquares(firstMax); // prints all squares to 5 printSquares(secondMax + 1); // prints all squares to 9 } } Monday, September 9, 13
  • 6. 6 Scoping methods public class ParameterTest { public static void printSquares(int maxSquare) { for (int i = 1; i <= maxSquare; i++) { System.out.println(i + " squared = " + i * i); } } public static void main(String[] args) { int firstMax = 5, secondMax = 8; printSquares(firstMax); // prints all squares to 5 printSquares(secondMax + 1); // prints all squares to 9 } } method main method printSquares method printSquares firstMax secondMax maxSquare maxSquare5 8 5 9 Monday, September 9, 13
  • 7. 7 Scoping methods • Be careful with the way you name parameters: public class ParameterExample { public static void main(String[] args) { int x = 4; doubleNumber(x); System.out.println("x = " + x); } public static void doubleNumber(int x) { System.out.println("Initial value = " + x); x *= 2; System.out.println("Final value = " + x); } } method main method doubleNumber x x4 48 Monday, September 9, 13