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
 
Lecture 3.6 bt
Lecture 3.6 btLecture 3.6 bt
Lecture 3.6 btbtmathematics
 
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
 
C# 8 and Beyond
C# 8 and BeyondC# 8 and Beyond
C# 8 and BeyondFilip Ekberg
 
functions
functionsfunctions
functionssmirn4
 
Graphing functions
Graphing functionsGraphing functions
Graphing functionskatiewilkerosn
 
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

Compensation imt1
Compensation imt1Compensation imt1
Compensation imt1Sonal Singhal
 
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
 
Employee Compensation
Employee CompensationEmployee Compensation
Employee CompensationSushant Murarka
 
Hr practices in ibm india
Hr practices in ibm indiaHr practices in ibm india
Hr practices in ibm indiaBhavana Rohidekar
 
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
 
Compensation practices
Compensation practicesCompensation practices
Compensation practicesSurya Srivastava
 
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
 
Ppt on tcs
Ppt on tcsPpt on tcs
Ppt on tcsRajnish Deo
 
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
 
McKinsey presentation
McKinsey presentationMcKinsey presentation
McKinsey presentationConstructingeq
 
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
 
14 thread
14 thread14 thread
14 threadBayarkhuu
 
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
 
STS4022 Exceptional_Handling
STS4022  Exceptional_HandlingSTS4022  Exceptional_Handling
STS4022 Exceptional_HandlingKeerthanaMadhavan19M
 
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
 
06slide.ppt
06slide.ppt06slide.ppt
06slide.pptRohitNukte
 
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
 
Java practical
Java practicalJava practical
Java practicalwilliam otto
 
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
 
Introduccion del curso
Introduccion del cursoIntroduccion del curso
Introduccion del cursoWilfredo Nieves
 

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

Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfJemuel Francisco
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Celine George
 
Science 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptxScience 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptxMaryGraceBautista27
 
4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptxmary850239
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPCeline George
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Celine George
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Mark Reed
 
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...Postal Advocate Inc.
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management systemChristalin Nelson
 
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITYISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITYKayeClaireEstoconing
 
ENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomnelietumpap1
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designMIPLM
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPCeline George
 
ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4MiaBumagat1
 
ACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfSpandanaRallapalli
 
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptxAUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptxiammrhaywood
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...JhezDiaz1
 

Recently uploaded (20)

Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
 
Science 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptxScience 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptx
 
4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERP
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)
 
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management system
 
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITYISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
 
ENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choom
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-design
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERP
 
ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4
 
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptxFINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
 
ACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdf
 
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptxAUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
 

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