SlideShare a Scribd company logo
1 of 33
LECTURE 5
BY: RUBYNA VOHRA
AGENDA
• Methods
• Parameters
METHODS
WHAT IS AN ALGORITHM?
• algorithm: A list of steps for solving a problem.
• Example: "Bake sugar cookies"
• Mix the dry ingredients.
• Cream the butter and sugar.
• Beat in the eggs.
• Stir in the dry ingredients.
• Set the oven temperature.
• Set the timer.
• Place the cookies into the oven.
• Allow the cookies to bake.
• Spread frosting and sprinkles onto the cookies.
PROBLEMS WITH ALGORITHMS
• Lack of structure: Many tiny steps; tough to remember.
• Redundancy: Consider making a double batch...
• Mix the dry ingredients.
• Cream the butter and sugar.
• Beat in the eggs.
• Stir in the dry ingredients.
• Set the oven temperature.
• ...
STATIC METHODS
• Static Method: A named group of statements.
• Denotes the structure of a program
• Eliminates redundancy by code reuse
• Procedural Decomposition: dividing a problem into methods
HOW TO USE STATIC METHODS
1. Design the Algorithm
• Which commands are repeated?
2. Declare (type) the methods
• Organize the repeated statements into groups
3. Call (run) the methods
• Main method will execute the methods to perform the overall task
SYNTAX: HOW TO DECLARE A METHOD IN C#
SYNTAX: HOW TO CALL A METHOD IN C#
HOW DO METHODS WORK (IN THE CODE)?
• When a method is called, the program's execution...
• "jumps" into that method, executing its statements, then…
• "jumps" back to the point where the method was called.
A METHOD CAN BE CALLED MULTIPLE TIMES:
LIVE WALKTHRU/EXPLANATION
PARAMETERS
REDUNDANT RECIPES
• Recipe for baking 20 cookies:
• Mix the following ingredients in a bowl:
• 4 cups flour
• 1 cup butter
• 1 cup sugar
• 2 eggs
• 1 bag chocolate chips .
• .. Place on sheet and Bake for about 10
minutes.
• Recipe for baking 40 cookies:
• Mix the following ingredients in a bowl:
• 8 cups flour
• 2 cup butter
• 2 cup sugar
• 4 eggs
• 2 bag chocolate chips .
• .. Place on sheet and Bake for about 10
minutes.
PARAMETERIZED RECIPE
• Recipe for baking N cookies:
• Mix the following ingredients in a bowl:
• N/5 cups flour
• N/20 cups butter
• N/20 cups sugar
• N/10 eggs
• N/20 bags chocolate chips
• ... Place on sheet and Bake for about 10 minutes.
• parameter: A value that distinguishes similar tasks.
REDUNDANT FIGURES
• Consider the task of printing the following lines/boxes:
*************
*******
***********************************
**********
* *
**********
*****
* *
* *
*****
PARAMETERIZATION
• parameter: A value passed to a method by its caller.
• Instead of lineOf7(), lineOf13(), write line to draw any length.
• When declaring the method,
• We will state that it requires a parameter for the number of stars.
• When calling the method, we will specify how many stars to draw.
DECLARING A PARAMETER
• Stating that a method requires a parameter in order to run
public static void name (type name) {
statement(s);
}
• Example:
public static void sayPassword(int code) {
Console.WriteLine("The password is: " + code);
}
• When sayPassword is called, the caller must specify the integer code to print.
PASSING PARAMETERS
• Calling a method and specifying values for its parameters
name (expression);
• Example:
public static void main(String[] args) {
sayPassword(42);
sayPassword(12345);
}
• Output:
The password is 42
The password is 12345
public static void chant(int repeat) {
Console.WriteLine(“I am chanting this sentence ” + repeat + “ times”);
}
COMMON ERRORS
• If a method accepts a parameter, it is illegal to call it without passing any value for
that parameter.
chant(); // ERROR: parameter value required
• The value passed to a method must be of the correct type.
chant(3.7); // ERROR: must be of type int
MULTIPLE PARAMETERS
• A method can accept multiple parameters. (separate by , )
• When calling it, you must pass values for each parameter.
• Declaration:
public static void name (type name, ..., type name) {
statement(s);
}
• Call:
methodName (value, value, ..., value);
LIVE WALKTHRU/EXPLANATION
DISCUSSION
• Why is this important?
• How can this be useful to me?
• How can I use this in my current
job?
• How can I use this in my future
career?
• What will I get out of this?
• How is this used in the real world
Regroup and discuss after…
COMPLETE THE
WORKSHEET WITH
YOUR GROUP
Regroup and discuss after…

More Related Content

Similar to Lecture 5

Educ416 week four_assignment
Educ416 week four_assignmentEduc416 week four_assignment
Educ416 week four_assignmentKathie Sedwick
 
PRACHI, 10T39B, PPT
PRACHI, 10T39B, PPTPRACHI, 10T39B, PPT
PRACHI, 10T39B, PPTPrachi Nagar
 
Introduction to TDD
Introduction to TDDIntroduction to TDD
Introduction to TDDNacho Cougil
 
PRAMOD @DESIGN SEM PRESENTATION.pptx
PRAMOD @DESIGN SEM PRESENTATION.pptxPRAMOD @DESIGN SEM PRESENTATION.pptx
PRAMOD @DESIGN SEM PRESENTATION.pptxPramodMada1
 
Algorithm week2(technovation)
Algorithm week2(technovation)Algorithm week2(technovation)
Algorithm week2(technovation)than sare
 
Recipe Management with InduSoft Web Studio
Recipe Management with InduSoft Web StudioRecipe Management with InduSoft Web Studio
Recipe Management with InduSoft Web StudioAVEVA
 
Basics of Functional Verification - Arrow Devices
Basics of Functional Verification - Arrow DevicesBasics of Functional Verification - Arrow Devices
Basics of Functional Verification - Arrow DevicesArrow Devices
 
Major Ingredients: www.chefqtrainer.blogspot.com
Major Ingredients: www.chefqtrainer.blogspot.comMajor Ingredients: www.chefqtrainer.blogspot.com
Major Ingredients: www.chefqtrainer.blogspot.comCulinary Training Program
 
Machining time and costs
Machining time and costsMachining time and costs
Machining time and costsendika55
 

Similar to Lecture 5 (13)

Educ416 week four_assignment
Educ416 week four_assignmentEduc416 week four_assignment
Educ416 week four_assignment
 
PRACHI, 10T39B, PPT
PRACHI, 10T39B, PPTPRACHI, 10T39B, PPT
PRACHI, 10T39B, PPT
 
Introduction to TDD
Introduction to TDDIntroduction to TDD
Introduction to TDD
 
TP8 Final vol 2_2
TP8 Final vol 2_2TP8 Final vol 2_2
TP8 Final vol 2_2
 
PRAMOD @DESIGN SEM PRESENTATION.pptx
PRAMOD @DESIGN SEM PRESENTATION.pptxPRAMOD @DESIGN SEM PRESENTATION.pptx
PRAMOD @DESIGN SEM PRESENTATION.pptx
 
Algorithm week2(technovation)
Algorithm week2(technovation)Algorithm week2(technovation)
Algorithm week2(technovation)
 
AS computing
AS computingAS computing
AS computing
 
TDD & VIPER
TDD & VIPERTDD & VIPER
TDD & VIPER
 
Recipe Management with InduSoft Web Studio
Recipe Management with InduSoft Web StudioRecipe Management with InduSoft Web Studio
Recipe Management with InduSoft Web Studio
 
Basics of Functional Verification - Arrow Devices
Basics of Functional Verification - Arrow DevicesBasics of Functional Verification - Arrow Devices
Basics of Functional Verification - Arrow Devices
 
Major Ingredients: www.chefqtrainer.blogspot.com
Major Ingredients: www.chefqtrainer.blogspot.comMajor Ingredients: www.chefqtrainer.blogspot.com
Major Ingredients: www.chefqtrainer.blogspot.com
 
Machining time and costs
Machining time and costsMachining time and costs
Machining time and costs
 
Ruby code smells
Ruby code smellsRuby code smells
Ruby code smells
 

More from Skillspire LLC (20)

Logistics
LogisticsLogistics
Logistics
 
Introduction to analytics
Introduction to analyticsIntroduction to analytics
Introduction to analytics
 
Lecture 31
Lecture 31Lecture 31
Lecture 31
 
Lecture 30
Lecture 30Lecture 30
Lecture 30
 
Lecture 29
Lecture 29Lecture 29
Lecture 29
 
Review
ReviewReview
Review
 
Review version 4
Review version 4Review version 4
Review version 4
 
Review version 3
Review version 3Review version 3
Review version 3
 
Review version 2
Review version 2Review version 2
Review version 2
 
Lecture 25
Lecture 25Lecture 25
Lecture 25
 
Lecture 24
Lecture 24Lecture 24
Lecture 24
 
Lecture 23 p1
Lecture 23 p1Lecture 23 p1
Lecture 23 p1
 
Lecture 21
Lecture 21Lecture 21
Lecture 21
 
Lecture 17
Lecture 17Lecture 17
Lecture 17
 
Lecture 16
Lecture 16Lecture 16
Lecture 16
 
Lecture 15
Lecture 15Lecture 15
Lecture 15
 
Lecture 14
Lecture 14Lecture 14
Lecture 14
 
Lecture 14
Lecture 14Lecture 14
Lecture 14
 
Lecture 13
Lecture 13Lecture 13
Lecture 13
 
Lecture 12
Lecture 12Lecture 12
Lecture 12
 

Recently uploaded

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
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
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
 
_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
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfSumit Tiwari
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 
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
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 

Recently uploaded (20)

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
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
Staff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSDStaff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSD
 
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
 
_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
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
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
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 
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
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 

Lecture 5

  • 2.
  • 5. WHAT IS AN ALGORITHM? • algorithm: A list of steps for solving a problem. • Example: "Bake sugar cookies" • Mix the dry ingredients. • Cream the butter and sugar. • Beat in the eggs. • Stir in the dry ingredients. • Set the oven temperature. • Set the timer. • Place the cookies into the oven. • Allow the cookies to bake. • Spread frosting and sprinkles onto the cookies.
  • 6. PROBLEMS WITH ALGORITHMS • Lack of structure: Many tiny steps; tough to remember. • Redundancy: Consider making a double batch... • Mix the dry ingredients. • Cream the butter and sugar. • Beat in the eggs. • Stir in the dry ingredients. • Set the oven temperature. • ...
  • 7.
  • 8.
  • 9. STATIC METHODS • Static Method: A named group of statements. • Denotes the structure of a program • Eliminates redundancy by code reuse • Procedural Decomposition: dividing a problem into methods
  • 10. HOW TO USE STATIC METHODS 1. Design the Algorithm • Which commands are repeated? 2. Declare (type) the methods • Organize the repeated statements into groups 3. Call (run) the methods • Main method will execute the methods to perform the overall task
  • 11. SYNTAX: HOW TO DECLARE A METHOD IN C#
  • 12. SYNTAX: HOW TO CALL A METHOD IN C#
  • 13. HOW DO METHODS WORK (IN THE CODE)? • When a method is called, the program's execution... • "jumps" into that method, executing its statements, then… • "jumps" back to the point where the method was called.
  • 14.
  • 15. A METHOD CAN BE CALLED MULTIPLE TIMES:
  • 17.
  • 19. REDUNDANT RECIPES • Recipe for baking 20 cookies: • Mix the following ingredients in a bowl: • 4 cups flour • 1 cup butter • 1 cup sugar • 2 eggs • 1 bag chocolate chips . • .. Place on sheet and Bake for about 10 minutes. • Recipe for baking 40 cookies: • Mix the following ingredients in a bowl: • 8 cups flour • 2 cup butter • 2 cup sugar • 4 eggs • 2 bag chocolate chips . • .. Place on sheet and Bake for about 10 minutes.
  • 20. PARAMETERIZED RECIPE • Recipe for baking N cookies: • Mix the following ingredients in a bowl: • N/5 cups flour • N/20 cups butter • N/20 cups sugar • N/10 eggs • N/20 bags chocolate chips • ... Place on sheet and Bake for about 10 minutes. • parameter: A value that distinguishes similar tasks.
  • 21. REDUNDANT FIGURES • Consider the task of printing the following lines/boxes: ************* ******* *********************************** ********** * * ********** ***** * * * * *****
  • 22. PARAMETERIZATION • parameter: A value passed to a method by its caller. • Instead of lineOf7(), lineOf13(), write line to draw any length. • When declaring the method, • We will state that it requires a parameter for the number of stars. • When calling the method, we will specify how many stars to draw.
  • 23. DECLARING A PARAMETER • Stating that a method requires a parameter in order to run public static void name (type name) { statement(s); } • Example: public static void sayPassword(int code) { Console.WriteLine("The password is: " + code); } • When sayPassword is called, the caller must specify the integer code to print.
  • 24. PASSING PARAMETERS • Calling a method and specifying values for its parameters name (expression); • Example: public static void main(String[] args) { sayPassword(42); sayPassword(12345); } • Output: The password is 42 The password is 12345
  • 25. public static void chant(int repeat) { Console.WriteLine(“I am chanting this sentence ” + repeat + “ times”); }
  • 26. COMMON ERRORS • If a method accepts a parameter, it is illegal to call it without passing any value for that parameter. chant(); // ERROR: parameter value required • The value passed to a method must be of the correct type. chant(3.7); // ERROR: must be of type int
  • 27. MULTIPLE PARAMETERS • A method can accept multiple parameters. (separate by , ) • When calling it, you must pass values for each parameter. • Declaration: public static void name (type name, ..., type name) { statement(s); } • Call: methodName (value, value, ..., value);
  • 29.
  • 30.
  • 31. DISCUSSION • Why is this important? • How can this be useful to me? • How can I use this in my current job? • How can I use this in my future career? • What will I get out of this? • How is this used in the real world Regroup and discuss after…
  • 32.
  • 33. COMPLETE THE WORKSHEET WITH YOUR GROUP Regroup and discuss after…