SlideShare a Scribd company logo
Methods
&
Parameters
Overview
 Using Methods
 Using Parameters
 Using Overloaded Methods
Using Methods
 Defining Methods
 Calling Methods
 Using the return Statement
 Using Local Variables
 Returning Values
Defining Methods
 Main is a method
 Use the same syntax for defining your own methods
using System;
class ExampleClass
{
static void ExampleMethod( )
{
Console.WriteLine("Example method");
}
static void Main( )
{
// ...
}
}
Calling Methods
 After you define a method, you can:
 Call a method from within the same class
 Use method’s name followed by a parameter list in parentheses
 Call a method that is in a different class
 You must indicate to the compiler which class contains the
method to call
 The called method must be declared with the public keyword
 Use nested calls
 Methods can call methods, which can call other methods, and so
on
Using the return Statement
 Immediate return
 Return with a conditional statement
static void ExampleMethod( )
{
int numBeans;
//...
Console.WriteLine("Hello");
if (numBeans < 10)
return;
Console.WriteLine("World");
}
Using Local Variables
 Local variables
 Created when method begins
 Private to the method
 Destroyed on exit
 Shared variables
 Class variables are used for sharing
 Scope conflicts
 Compiler will not warn if local and class names clash
Returning Values
 Declare the method with non-void type
 Add a return statement with an expression
 Sets the return value
 Returns to caller
 Non-void methods must return a value
static int TwoPlusTwo( ) {
int a,b;
a = 2;
b = 2;
return a + b;
}
int x;
x = TwoPlusTwo( );
Console.WriteLine(x);
Using Parameters
 Declaring and Calling Parameters
 Mechanisms for Passing Parameters
 Pass by Value
 Pass by Reference
 Output Parameters
 Using Variable-Length Parameter Lists
 Guidelines for Passing Parameters
 Using Recursive Methods
Declaring and Calling Parameters
 Declaring parameters
 Place between parentheses after method name
 Define type and name for each parameter
 Calling methods with parameters
 Supply a value for each parameter
static void MethodWithParameters(int n, string y)
{ ... }
MethodWithParameters(2, "Hello, world");
Mechanisms for Passing Parameters
 Three ways to pass parameters
in Pass by value
in
out
Pass by reference
out Output parameters
Pass by Value
 Default mechanism for passing parameters:
 Parameter value is copied
 Variable can be changed inside the method
 Has no effect on value outside the method
 Parameter must be of the same type or compatible type
static void AddOne(int x)
{
x++; // Increment x
}
static void Main( )
{
int k = 6;
AddOne(k);
Console.WriteLine(k); // Display the value 6, not 7
}
Pass by Reference
 What are reference parameters?
 A reference to memory location
 Using reference parameters
 Use the ref keyword in method declaration and call
 Match types and variable values
 Changes made in the method affect the caller
 Assign parameter value before calling the method
Output Parameters
 What are output parameters?
 Values are passed out but not in
 Using output parameters
 Like ref, but values are not passed into the method
 Use out keyword in method declaration and call
static void OutDemo(out int p)
{
// ...
}
int n;
OutDemo(out n);
Using Variable-Length Parameter
Lists
 Use the params keyword
 Declare as an array at the end of the parameter list
 Always pass by value
static long AddList(params long[ ] v)
{
long total, i;
for (i = 0, total = 0; i < v.Length; i++)
total += v[i];
return total;
}
static void Main( )
{
long x = AddList(63,21,84);
}
Guidelines for Passing
Parameters
 Mechanisms
 Pass by value is most common
 Method return value is useful for single values
 Use ref and/or out for multiple return values
 Only use ref if data is transferred both ways
 Efficiency
 Pass by value is generally the most efficient
Using Recursive Methods
 A method can call itself
 Directly
 Indirectly
 Useful for solving certain problems
Using Overloaded Methods
 Declaring overloaded methods
 Method signatures
 Using overloaded methods
Declaring Overloaded Methods
 Methods that share a name in a class
 Distinguished by examining parameter lists
class OverloadingExample
{
static int Add(int a, int b)
{
return a + b;
}
static int Add(int a, int b, int c)
{
return a + b + c;
}
static void Main( )
{
Console.WriteLine(Add(1,2) + Add(1,2,3));
}
}
Method Signatures
 Method signatures must be unique within a class
 Signature definition
 Name of method
 Parameter type
 Parameter modifier
Forms Signature
Definition
 Name of parameter
 Return type of method
No Effect on
Signature
Using Overloaded Methods
 Consider using overloaded methods when:
 You have similar methods that require different parameters
 You want to add new functionality to existing code
 Do not overuse because:
 Hard to debug
 Hard to maintain
Review
 Using Methods
 Using Parameters
 Using Overloaded Methods

More Related Content

What's hot

Chapter 6.5
Chapter 6.5Chapter 6.5
Chapter 6.5
sotlsoc
 
Java method present by showrov ahamed
Java method present by showrov ahamedJava method present by showrov ahamed
Java method present by showrov ahamed
Md Showrov Ahmed
 
Methods In C-Sharp (C#)
Methods In C-Sharp (C#)Methods In C-Sharp (C#)
Methods In C-Sharp (C#)
Abid Kohistani
 
Method overloading
Method overloadingMethod overloading
Method overloading
Lovely Professional University
 
Vectors
VectorsVectors
Vectors
PTCL
 
Chapter 6.5 new
Chapter 6.5 newChapter 6.5 new
Chapter 6.5 new
sotlsoc
 
Chapter 3.3
Chapter 3.3Chapter 3.3
Chapter 3.3
sotlsoc
 
Java Methods
Java MethodsJava Methods
Java Methods
OXUS 20
 
Ch06
Ch06Ch06
Chapter 03
Chapter 03Chapter 03
Chapter 03
Chaffey College
 
Chapter 05
Chapter 05Chapter 05
Chapter 05
Chaffey College
 
Chapter 04
Chapter 04Chapter 04
Chapter 04
Chaffey College
 
Intake 37 2
Intake 37 2Intake 37 2
Intake 37 2
Mahmoud Ouf
 
Lambda Expressions in C# From Beginner To Expert - Jaliya Udagedara
Lambda Expressions in C# From Beginner To Expert - Jaliya UdagedaraLambda Expressions in C# From Beginner To Expert - Jaliya Udagedara
Lambda Expressions in C# From Beginner To Expert - Jaliya Udagedara
Jaliya Udagedara
 
Java 8 Functional Programming - I
Java 8 Functional Programming - IJava 8 Functional Programming - I
Java 8 Functional Programming - I
Ugur Yeter
 
Class 4: Making Procedures
Class 4: Making ProceduresClass 4: Making Procedures
Class 4: Making Procedures
David Evans
 
Intake 37 1
Intake 37 1Intake 37 1
Intake 37 1
Mahmoud Ouf
 
Java Tutorial Lab 3
Java Tutorial Lab 3Java Tutorial Lab 3
Java Tutorial Lab 3
Berk Soysal
 
CIS-166 Midterm
CIS-166 MidtermCIS-166 Midterm
Array
ArrayArray
Array
PRN USM
 

What's hot (20)

Chapter 6.5
Chapter 6.5Chapter 6.5
Chapter 6.5
 
Java method present by showrov ahamed
Java method present by showrov ahamedJava method present by showrov ahamed
Java method present by showrov ahamed
 
Methods In C-Sharp (C#)
Methods In C-Sharp (C#)Methods In C-Sharp (C#)
Methods In C-Sharp (C#)
 
Method overloading
Method overloadingMethod overloading
Method overloading
 
Vectors
VectorsVectors
Vectors
 
Chapter 6.5 new
Chapter 6.5 newChapter 6.5 new
Chapter 6.5 new
 
Chapter 3.3
Chapter 3.3Chapter 3.3
Chapter 3.3
 
Java Methods
Java MethodsJava Methods
Java Methods
 
Ch06
Ch06Ch06
Ch06
 
Chapter 03
Chapter 03Chapter 03
Chapter 03
 
Chapter 05
Chapter 05Chapter 05
Chapter 05
 
Chapter 04
Chapter 04Chapter 04
Chapter 04
 
Intake 37 2
Intake 37 2Intake 37 2
Intake 37 2
 
Lambda Expressions in C# From Beginner To Expert - Jaliya Udagedara
Lambda Expressions in C# From Beginner To Expert - Jaliya UdagedaraLambda Expressions in C# From Beginner To Expert - Jaliya Udagedara
Lambda Expressions in C# From Beginner To Expert - Jaliya Udagedara
 
Java 8 Functional Programming - I
Java 8 Functional Programming - IJava 8 Functional Programming - I
Java 8 Functional Programming - I
 
Class 4: Making Procedures
Class 4: Making ProceduresClass 4: Making Procedures
Class 4: Making Procedures
 
Intake 37 1
Intake 37 1Intake 37 1
Intake 37 1
 
Java Tutorial Lab 3
Java Tutorial Lab 3Java Tutorial Lab 3
Java Tutorial Lab 3
 
CIS-166 Midterm
CIS-166 MidtermCIS-166 Midterm
CIS-166 Midterm
 
Array
ArrayArray
Array
 

Similar to Module 4 : methods & parameters

Class 10
Class 10Class 10
JAVA METHOD AND FUNCTION DIVIDE AND SHORT.pptx
JAVA METHOD AND FUNCTION DIVIDE AND SHORT.pptxJAVA METHOD AND FUNCTION DIVIDE AND SHORT.pptx
JAVA METHOD AND FUNCTION DIVIDE AND SHORT.pptx
JohnMarkDeJesus4
 
Delegate
DelegateDelegate
Delegate
abhay singh
 
09. Methods
09. Methods09. Methods
09. Methods
Intro C# Book
 
Java Foundations: Methods
Java Foundations: MethodsJava Foundations: Methods
Java Foundations: Methods
Svetlin Nakov
 
Methods intro-1.0
Methods intro-1.0Methods intro-1.0
Methods intro-1.0
BG Java EE Course
 
11. java methods
11. java methods11. java methods
11. java methods
M H Buddhika Ariyaratne
 
EContent_11_2023_04_09_11_30_38_Unit_3_Objects_and_Classespptx__2023_03_20_12...
EContent_11_2023_04_09_11_30_38_Unit_3_Objects_and_Classespptx__2023_03_20_12...EContent_11_2023_04_09_11_30_38_Unit_3_Objects_and_Classespptx__2023_03_20_12...
EContent_11_2023_04_09_11_30_38_Unit_3_Objects_and_Classespptx__2023_03_20_12...
vekariyakashyap
 
Methods in Java
Methods in JavaMethods in Java
Methods in Java
Kavitha713564
 
Java -lec-6
Java -lec-6Java -lec-6
Java -lec-6
Zubair Khalid
 
Working with Methods in Java.pptx
Working with Methods in Java.pptxWorking with Methods in Java.pptx
Working with Methods in Java.pptx
maryansagsgao
 
Bad Smell in Codes - Part 1
Bad Smell in Codes - Part 1Bad Smell in Codes - Part 1
Bad Smell in Codes - Part 1
Shaer Hassan
 
Visula C# Programming Lecture 6
Visula C# Programming Lecture 6Visula C# Programming Lecture 6
Visula C# Programming Lecture 6
Abou Bakr Ashraf
 
Procedures functions structures in VB.Net
Procedures  functions  structures in VB.NetProcedures  functions  structures in VB.Net
Procedures functions structures in VB.Net
tjunicornfx
 
Method parameters in C# - All types of parameter passing in C #
Method parameters in C# - All types of parameter passing in C #Method parameters in C# - All types of parameter passing in C #
Method parameters in C# - All types of parameter passing in C #
JayanthiM19
 
Encapsulating method details in a class [ Choose ] instance vari.pdf
Encapsulating method details in a class [ Choose ] instance vari.pdfEncapsulating method details in a class [ Choose ] instance vari.pdf
Encapsulating method details in a class [ Choose ] instance vari.pdf
Conint29
 
Lecture07 computer applicationsie1_dratifshahzad
Lecture07 computer applicationsie1_dratifshahzadLecture07 computer applicationsie1_dratifshahzad
Lecture07 computer applicationsie1_dratifshahzad
Atif Shahzad
 
14method in c#
14method in c#14method in c#
14method in c#
Sireesh K
 
Lecture 4_Java Method-constructor_imp_keywords
Lecture   4_Java Method-constructor_imp_keywordsLecture   4_Java Method-constructor_imp_keywords
Lecture 4_Java Method-constructor_imp_keywords
manish kumar
 
Intake 38 2
Intake 38 2Intake 38 2
Intake 38 2
Mahmoud Ouf
 

Similar to Module 4 : methods & parameters (20)

Class 10
Class 10Class 10
Class 10
 
JAVA METHOD AND FUNCTION DIVIDE AND SHORT.pptx
JAVA METHOD AND FUNCTION DIVIDE AND SHORT.pptxJAVA METHOD AND FUNCTION DIVIDE AND SHORT.pptx
JAVA METHOD AND FUNCTION DIVIDE AND SHORT.pptx
 
Delegate
DelegateDelegate
Delegate
 
09. Methods
09. Methods09. Methods
09. Methods
 
Java Foundations: Methods
Java Foundations: MethodsJava Foundations: Methods
Java Foundations: Methods
 
Methods intro-1.0
Methods intro-1.0Methods intro-1.0
Methods intro-1.0
 
11. java methods
11. java methods11. java methods
11. java methods
 
EContent_11_2023_04_09_11_30_38_Unit_3_Objects_and_Classespptx__2023_03_20_12...
EContent_11_2023_04_09_11_30_38_Unit_3_Objects_and_Classespptx__2023_03_20_12...EContent_11_2023_04_09_11_30_38_Unit_3_Objects_and_Classespptx__2023_03_20_12...
EContent_11_2023_04_09_11_30_38_Unit_3_Objects_and_Classespptx__2023_03_20_12...
 
Methods in Java
Methods in JavaMethods in Java
Methods in Java
 
Java -lec-6
Java -lec-6Java -lec-6
Java -lec-6
 
Working with Methods in Java.pptx
Working with Methods in Java.pptxWorking with Methods in Java.pptx
Working with Methods in Java.pptx
 
Bad Smell in Codes - Part 1
Bad Smell in Codes - Part 1Bad Smell in Codes - Part 1
Bad Smell in Codes - Part 1
 
Visula C# Programming Lecture 6
Visula C# Programming Lecture 6Visula C# Programming Lecture 6
Visula C# Programming Lecture 6
 
Procedures functions structures in VB.Net
Procedures  functions  structures in VB.NetProcedures  functions  structures in VB.Net
Procedures functions structures in VB.Net
 
Method parameters in C# - All types of parameter passing in C #
Method parameters in C# - All types of parameter passing in C #Method parameters in C# - All types of parameter passing in C #
Method parameters in C# - All types of parameter passing in C #
 
Encapsulating method details in a class [ Choose ] instance vari.pdf
Encapsulating method details in a class [ Choose ] instance vari.pdfEncapsulating method details in a class [ Choose ] instance vari.pdf
Encapsulating method details in a class [ Choose ] instance vari.pdf
 
Lecture07 computer applicationsie1_dratifshahzad
Lecture07 computer applicationsie1_dratifshahzadLecture07 computer applicationsie1_dratifshahzad
Lecture07 computer applicationsie1_dratifshahzad
 
14method in c#
14method in c#14method in c#
14method in c#
 
Lecture 4_Java Method-constructor_imp_keywords
Lecture   4_Java Method-constructor_imp_keywordsLecture   4_Java Method-constructor_imp_keywords
Lecture 4_Java Method-constructor_imp_keywords
 
Intake 38 2
Intake 38 2Intake 38 2
Intake 38 2
 

More from Prem Kumar Badri

Module 15 attributes
Module 15 attributesModule 15 attributes
Module 15 attributes
Prem Kumar Badri
 
Module 14 properties and indexers
Module 14 properties and indexersModule 14 properties and indexers
Module 14 properties and indexers
Prem Kumar Badri
 
Module 12 aggregation, namespaces, and advanced scope
Module 12 aggregation, namespaces, and advanced scopeModule 12 aggregation, namespaces, and advanced scope
Module 12 aggregation, namespaces, and advanced scope
Prem Kumar Badri
 
Module 13 operators, delegates, and events
Module 13 operators, delegates, and eventsModule 13 operators, delegates, and events
Module 13 operators, delegates, and events
Prem Kumar Badri
 
Module 11 : Inheritance
Module 11 : InheritanceModule 11 : Inheritance
Module 11 : Inheritance
Prem Kumar Badri
 
Module 10 : creating and destroying objects
Module 10 : creating and destroying objectsModule 10 : creating and destroying objects
Module 10 : creating and destroying objects
Prem Kumar Badri
 
Module 9 : using reference type variables
Module 9 : using reference type variablesModule 9 : using reference type variables
Module 9 : using reference type variables
Prem Kumar Badri
 
Module 8 : Implementing collections and generics
Module 8 : Implementing collections and genericsModule 8 : Implementing collections and generics
Module 8 : Implementing collections and generics
Prem Kumar Badri
 
Module 7 : Arrays
Module 7 : ArraysModule 7 : Arrays
Module 7 : Arrays
Prem Kumar Badri
 
Module 6 : Essentials of Object Oriented Programming
Module 6 : Essentials of Object Oriented ProgrammingModule 6 : Essentials of Object Oriented Programming
Module 6 : Essentials of Object Oriented Programming
Prem Kumar Badri
 
Module 5 : Statements & Exceptions
Module 5 : Statements & ExceptionsModule 5 : Statements & Exceptions
Module 5 : Statements & Exceptions
Prem Kumar Badri
 
Module 3 : using value type variables
Module 3 : using value type variablesModule 3 : using value type variables
Module 3 : using value type variables
Prem Kumar Badri
 
Module 2: Overview of c#
Module 2:  Overview of c#Module 2:  Overview of c#
Module 2: Overview of c#
Prem Kumar Badri
 
Module 1 : Overview of the Microsoft .NET Platform
Module 1 : Overview of the Microsoft .NET PlatformModule 1 : Overview of the Microsoft .NET Platform
Module 1 : Overview of the Microsoft .NET Platform
Prem Kumar Badri
 
C# Non generics collection
C# Non generics collectionC# Non generics collection
C# Non generics collection
Prem Kumar Badri
 
C# Multi threading
C# Multi threadingC# Multi threading
C# Multi threading
Prem Kumar Badri
 
C# Method overloading
C# Method overloadingC# Method overloading
C# Method overloading
Prem Kumar Badri
 
C# Inheritance
C# InheritanceC# Inheritance
C# Inheritance
Prem Kumar Badri
 
C# Generic collections
C# Generic collectionsC# Generic collections
C# Generic collections
Prem Kumar Badri
 
C# Global Assembly Cache
C# Global Assembly CacheC# Global Assembly Cache
C# Global Assembly Cache
Prem Kumar Badri
 

More from Prem Kumar Badri (20)

Module 15 attributes
Module 15 attributesModule 15 attributes
Module 15 attributes
 
Module 14 properties and indexers
Module 14 properties and indexersModule 14 properties and indexers
Module 14 properties and indexers
 
Module 12 aggregation, namespaces, and advanced scope
Module 12 aggregation, namespaces, and advanced scopeModule 12 aggregation, namespaces, and advanced scope
Module 12 aggregation, namespaces, and advanced scope
 
Module 13 operators, delegates, and events
Module 13 operators, delegates, and eventsModule 13 operators, delegates, and events
Module 13 operators, delegates, and events
 
Module 11 : Inheritance
Module 11 : InheritanceModule 11 : Inheritance
Module 11 : Inheritance
 
Module 10 : creating and destroying objects
Module 10 : creating and destroying objectsModule 10 : creating and destroying objects
Module 10 : creating and destroying objects
 
Module 9 : using reference type variables
Module 9 : using reference type variablesModule 9 : using reference type variables
Module 9 : using reference type variables
 
Module 8 : Implementing collections and generics
Module 8 : Implementing collections and genericsModule 8 : Implementing collections and generics
Module 8 : Implementing collections and generics
 
Module 7 : Arrays
Module 7 : ArraysModule 7 : Arrays
Module 7 : Arrays
 
Module 6 : Essentials of Object Oriented Programming
Module 6 : Essentials of Object Oriented ProgrammingModule 6 : Essentials of Object Oriented Programming
Module 6 : Essentials of Object Oriented Programming
 
Module 5 : Statements & Exceptions
Module 5 : Statements & ExceptionsModule 5 : Statements & Exceptions
Module 5 : Statements & Exceptions
 
Module 3 : using value type variables
Module 3 : using value type variablesModule 3 : using value type variables
Module 3 : using value type variables
 
Module 2: Overview of c#
Module 2:  Overview of c#Module 2:  Overview of c#
Module 2: Overview of c#
 
Module 1 : Overview of the Microsoft .NET Platform
Module 1 : Overview of the Microsoft .NET PlatformModule 1 : Overview of the Microsoft .NET Platform
Module 1 : Overview of the Microsoft .NET Platform
 
C# Non generics collection
C# Non generics collectionC# Non generics collection
C# Non generics collection
 
C# Multi threading
C# Multi threadingC# Multi threading
C# Multi threading
 
C# Method overloading
C# Method overloadingC# Method overloading
C# Method overloading
 
C# Inheritance
C# InheritanceC# Inheritance
C# Inheritance
 
C# Generic collections
C# Generic collectionsC# Generic collections
C# Generic collections
 
C# Global Assembly Cache
C# Global Assembly CacheC# Global Assembly Cache
C# Global Assembly Cache
 

Recently uploaded

Wound healing PPT
Wound healing PPTWound healing PPT
Wound healing PPT
Jyoti Chand
 
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptxPrésentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
siemaillard
 
South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)
Academy of Science of South Africa
 
Film vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movieFilm vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movie
Nicholas Montgomery
 
clinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdfclinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdf
Priyankaranawat4
 
How to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 InventoryHow to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 Inventory
Celine George
 
Pengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptxPengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptx
Fajar Baskoro
 
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptxNEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
iammrhaywood
 
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
GeorgeMilliken2
 
Cognitive Development Adolescence Psychology
Cognitive Development Adolescence PsychologyCognitive Development Adolescence Psychology
Cognitive Development Adolescence Psychology
paigestewart1632
 
Hindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdfHindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdf
Dr. Mulla Adam Ali
 
Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...
Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...
Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...
imrankhan141184
 
How to deliver Powerpoint Presentations.pptx
How to deliver Powerpoint  Presentations.pptxHow to deliver Powerpoint  Presentations.pptx
How to deliver Powerpoint Presentations.pptx
HajraNaeem15
 
คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1
คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1
คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1
สมใจ จันสุกสี
 
BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...
BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...
BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...
Nguyen Thanh Tu Collection
 
Beyond Degrees - Empowering the Workforce in the Context of Skills-First.pptx
Beyond Degrees - Empowering the Workforce in the Context of Skills-First.pptxBeyond Degrees - Empowering the Workforce in the Context of Skills-First.pptx
Beyond Degrees - Empowering the Workforce in the Context of Skills-First.pptx
EduSkills OECD
 
Leveraging Generative AI to Drive Nonprofit Innovation
Leveraging Generative AI to Drive Nonprofit InnovationLeveraging Generative AI to Drive Nonprofit Innovation
Leveraging Generative AI to Drive Nonprofit Innovation
TechSoup
 
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
National Information Standards Organization (NISO)
 
Liberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdfLiberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdf
WaniBasim
 
A Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdfA Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdf
Jean Carlos Nunes Paixão
 

Recently uploaded (20)

Wound healing PPT
Wound healing PPTWound healing PPT
Wound healing PPT
 
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptxPrésentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
 
South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)
 
Film vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movieFilm vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movie
 
clinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdfclinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdf
 
How to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 InventoryHow to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 Inventory
 
Pengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptxPengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptx
 
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptxNEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
 
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
 
Cognitive Development Adolescence Psychology
Cognitive Development Adolescence PsychologyCognitive Development Adolescence Psychology
Cognitive Development Adolescence Psychology
 
Hindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdfHindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdf
 
Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...
Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...
Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...
 
How to deliver Powerpoint Presentations.pptx
How to deliver Powerpoint  Presentations.pptxHow to deliver Powerpoint  Presentations.pptx
How to deliver Powerpoint Presentations.pptx
 
คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1
คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1
คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1
 
BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...
BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...
BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...
 
Beyond Degrees - Empowering the Workforce in the Context of Skills-First.pptx
Beyond Degrees - Empowering the Workforce in the Context of Skills-First.pptxBeyond Degrees - Empowering the Workforce in the Context of Skills-First.pptx
Beyond Degrees - Empowering the Workforce in the Context of Skills-First.pptx
 
Leveraging Generative AI to Drive Nonprofit Innovation
Leveraging Generative AI to Drive Nonprofit InnovationLeveraging Generative AI to Drive Nonprofit Innovation
Leveraging Generative AI to Drive Nonprofit Innovation
 
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
 
Liberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdfLiberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdf
 
A Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdfA Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdf
 

Module 4 : methods & parameters

  • 2. Overview  Using Methods  Using Parameters  Using Overloaded Methods
  • 3. Using Methods  Defining Methods  Calling Methods  Using the return Statement  Using Local Variables  Returning Values
  • 4. Defining Methods  Main is a method  Use the same syntax for defining your own methods using System; class ExampleClass { static void ExampleMethod( ) { Console.WriteLine("Example method"); } static void Main( ) { // ... } }
  • 5. Calling Methods  After you define a method, you can:  Call a method from within the same class  Use method’s name followed by a parameter list in parentheses  Call a method that is in a different class  You must indicate to the compiler which class contains the method to call  The called method must be declared with the public keyword  Use nested calls  Methods can call methods, which can call other methods, and so on
  • 6. Using the return Statement  Immediate return  Return with a conditional statement static void ExampleMethod( ) { int numBeans; //... Console.WriteLine("Hello"); if (numBeans < 10) return; Console.WriteLine("World"); }
  • 7. Using Local Variables  Local variables  Created when method begins  Private to the method  Destroyed on exit  Shared variables  Class variables are used for sharing  Scope conflicts  Compiler will not warn if local and class names clash
  • 8. Returning Values  Declare the method with non-void type  Add a return statement with an expression  Sets the return value  Returns to caller  Non-void methods must return a value static int TwoPlusTwo( ) { int a,b; a = 2; b = 2; return a + b; } int x; x = TwoPlusTwo( ); Console.WriteLine(x);
  • 9. Using Parameters  Declaring and Calling Parameters  Mechanisms for Passing Parameters  Pass by Value  Pass by Reference  Output Parameters  Using Variable-Length Parameter Lists  Guidelines for Passing Parameters  Using Recursive Methods
  • 10. Declaring and Calling Parameters  Declaring parameters  Place between parentheses after method name  Define type and name for each parameter  Calling methods with parameters  Supply a value for each parameter static void MethodWithParameters(int n, string y) { ... } MethodWithParameters(2, "Hello, world");
  • 11. Mechanisms for Passing Parameters  Three ways to pass parameters in Pass by value in out Pass by reference out Output parameters
  • 12. Pass by Value  Default mechanism for passing parameters:  Parameter value is copied  Variable can be changed inside the method  Has no effect on value outside the method  Parameter must be of the same type or compatible type static void AddOne(int x) { x++; // Increment x } static void Main( ) { int k = 6; AddOne(k); Console.WriteLine(k); // Display the value 6, not 7 }
  • 13. Pass by Reference  What are reference parameters?  A reference to memory location  Using reference parameters  Use the ref keyword in method declaration and call  Match types and variable values  Changes made in the method affect the caller  Assign parameter value before calling the method
  • 14. Output Parameters  What are output parameters?  Values are passed out but not in  Using output parameters  Like ref, but values are not passed into the method  Use out keyword in method declaration and call static void OutDemo(out int p) { // ... } int n; OutDemo(out n);
  • 15. Using Variable-Length Parameter Lists  Use the params keyword  Declare as an array at the end of the parameter list  Always pass by value static long AddList(params long[ ] v) { long total, i; for (i = 0, total = 0; i < v.Length; i++) total += v[i]; return total; } static void Main( ) { long x = AddList(63,21,84); }
  • 16. Guidelines for Passing Parameters  Mechanisms  Pass by value is most common  Method return value is useful for single values  Use ref and/or out for multiple return values  Only use ref if data is transferred both ways  Efficiency  Pass by value is generally the most efficient
  • 17. Using Recursive Methods  A method can call itself  Directly  Indirectly  Useful for solving certain problems
  • 18. Using Overloaded Methods  Declaring overloaded methods  Method signatures  Using overloaded methods
  • 19. Declaring Overloaded Methods  Methods that share a name in a class  Distinguished by examining parameter lists class OverloadingExample { static int Add(int a, int b) { return a + b; } static int Add(int a, int b, int c) { return a + b + c; } static void Main( ) { Console.WriteLine(Add(1,2) + Add(1,2,3)); } }
  • 20. Method Signatures  Method signatures must be unique within a class  Signature definition  Name of method  Parameter type  Parameter modifier Forms Signature Definition  Name of parameter  Return type of method No Effect on Signature
  • 21. Using Overloaded Methods  Consider using overloaded methods when:  You have similar methods that require different parameters  You want to add new functionality to existing code  Do not overuse because:  Hard to debug  Hard to maintain
  • 22. Review  Using Methods  Using Parameters  Using Overloaded Methods