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 (20)

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

Similar to Module 4 : methods & parameters (20)

PPTX
Class 10
SIVASHANKARIRAJAN
 
PPTX
JAVA METHOD AND FUNCTION DIVIDE AND SHORT.pptx
JohnMarkDeJesus4
 
PPT
Delegate
abhay singh
 
PPTX
09. Methods
Intro C# Book
 
PPTX
Java Foundations: Methods
Svetlin Nakov
 
PPT
Methods intro-1.0
BG Java EE Course
 
PPTX
11. java methods
M H Buddhika Ariyaratne
 
PPTX
EContent_11_2023_04_09_11_30_38_Unit_3_Objects_and_Classespptx__2023_03_20_12...
vekariyakashyap
 
DOCX
Methods in Java
Kavitha713564
 
PDF
Java -lec-6
Zubair Khalid
 
PPTX
IntroductionJava Programming - Math Class
sandhyakiran10
 
PPTX
Working with Methods in Java.pptx
maryansagsgao
 
PPT
Bad Smell in Codes - Part 1
Shaer Hassan
 
PPT
Visula C# Programming Lecture 6
Abou Bakr Ashraf
 
PPS
Procedures functions structures in VB.Net
tjunicornfx
 
PPTX
Method parameters in C# - All types of parameter passing in C #
JayanthiM19
 
PDF
Encapsulating method details in a class [ Choose ] instance vari.pdf
Conint29
 
PDF
Lecture07 computer applicationsie1_dratifshahzad
Atif Shahzad
 
PPTX
14method in c#
Sireesh K
 
PPTX
Lecture 4_Java Method-constructor_imp_keywords
manish kumar
 
JAVA METHOD AND FUNCTION DIVIDE AND SHORT.pptx
JohnMarkDeJesus4
 
Delegate
abhay singh
 
09. Methods
Intro C# Book
 
Java Foundations: Methods
Svetlin Nakov
 
Methods intro-1.0
BG Java EE Course
 
11. java methods
M H Buddhika Ariyaratne
 
EContent_11_2023_04_09_11_30_38_Unit_3_Objects_and_Classespptx__2023_03_20_12...
vekariyakashyap
 
Methods in Java
Kavitha713564
 
Java -lec-6
Zubair Khalid
 
IntroductionJava Programming - Math Class
sandhyakiran10
 
Working with Methods in Java.pptx
maryansagsgao
 
Bad Smell in Codes - Part 1
Shaer Hassan
 
Visula C# Programming Lecture 6
Abou Bakr Ashraf
 
Procedures functions structures in VB.Net
tjunicornfx
 
Method parameters in C# - All types of parameter passing in C #
JayanthiM19
 
Encapsulating method details in a class [ Choose ] instance vari.pdf
Conint29
 
Lecture07 computer applicationsie1_dratifshahzad
Atif Shahzad
 
14method in c#
Sireesh K
 
Lecture 4_Java Method-constructor_imp_keywords
manish kumar
 
Ad

More from Prem Kumar Badri (20)

PPTX
Module 15 attributes
Prem Kumar Badri
 
PPTX
Module 14 properties and indexers
Prem Kumar Badri
 
PPTX
Module 12 aggregation, namespaces, and advanced scope
Prem Kumar Badri
 
PPTX
Module 13 operators, delegates, and events
Prem Kumar Badri
 
PPTX
Module 11 : Inheritance
Prem Kumar Badri
 
PPTX
Module 10 : creating and destroying objects
Prem Kumar Badri
 
PPTX
Module 9 : using reference type variables
Prem Kumar Badri
 
PPTX
Module 8 : Implementing collections and generics
Prem Kumar Badri
 
PPTX
Module 7 : Arrays
Prem Kumar Badri
 
PPTX
Module 6 : Essentials of Object Oriented Programming
Prem Kumar Badri
 
PPTX
Module 5 : Statements & Exceptions
Prem Kumar Badri
 
PPTX
Module 3 : using value type variables
Prem Kumar Badri
 
PPTX
Module 2: Overview of c#
Prem Kumar Badri
 
PPTX
Module 1 : Overview of the Microsoft .NET Platform
Prem Kumar Badri
 
PPTX
C# Non generics collection
Prem Kumar Badri
 
PPTX
C# Multi threading
Prem Kumar Badri
 
PPT
C# Method overloading
Prem Kumar Badri
 
PPTX
C# Inheritance
Prem Kumar Badri
 
PPTX
C# Generic collections
Prem Kumar Badri
 
PPTX
C# Global Assembly Cache
Prem Kumar Badri
 
Module 15 attributes
Prem Kumar Badri
 
Module 14 properties and indexers
Prem Kumar Badri
 
Module 12 aggregation, namespaces, and advanced scope
Prem Kumar Badri
 
Module 13 operators, delegates, and events
Prem Kumar Badri
 
Module 11 : Inheritance
Prem Kumar Badri
 
Module 10 : creating and destroying objects
Prem Kumar Badri
 
Module 9 : using reference type variables
Prem Kumar Badri
 
Module 8 : Implementing collections and generics
Prem Kumar Badri
 
Module 7 : Arrays
Prem Kumar Badri
 
Module 6 : Essentials of Object Oriented Programming
Prem Kumar Badri
 
Module 5 : Statements & Exceptions
Prem Kumar Badri
 
Module 3 : using value type variables
Prem Kumar Badri
 
Module 2: Overview of c#
Prem Kumar Badri
 
Module 1 : Overview of the Microsoft .NET Platform
Prem Kumar Badri
 
C# Non generics collection
Prem Kumar Badri
 
C# Multi threading
Prem Kumar Badri
 
C# Method overloading
Prem Kumar Badri
 
C# Inheritance
Prem Kumar Badri
 
C# Generic collections
Prem Kumar Badri
 
C# Global Assembly Cache
Prem Kumar Badri
 
Ad

Recently uploaded (20)

PDF
CONCURSO DE POESIA “POETUFAS – PASSOS SUAVES PELO VERSO.pdf
Colégio Santa Teresinha
 
PDF
The dynastic history of the Chahmana.pdf
PrachiSontakke5
 
PPTX
2025 Winter SWAYAM NPTEL & A Student.pptx
Utsav Yagnik
 
PPTX
PATIENT ASSIGNMENTS AND NURSING CARE RESPONSIBILITIES.pptx
PRADEEP ABOTHU
 
PDF
Stokey: A Jewish Village by Rachel Kolsky
History of Stoke Newington
 
PDF
LAW OF CONTRACT (5 YEAR LLB & UNITARY LLB )- MODULE - 1.& 2 - LEARN THROUGH P...
APARNA T SHAIL KUMAR
 
PPTX
Stereochemistry-Optical Isomerism in organic compoundsptx
Tarannum Nadaf-Mansuri
 
PPTX
How to Set Maximum Difference Odoo 18 POS
Celine George
 
PDF
Generative AI: it's STILL not a robot (CIJ Summer 2025)
Paul Bradshaw
 
PPTX
Unit 2 COMMERCIAL BANKING, Corporate banking.pptx
AnubalaSuresh1
 
PPTX
SPINA BIFIDA: NURSING MANAGEMENT .pptx
PRADEEP ABOTHU
 
PPTX
STAFF DEVELOPMENT AND WELFARE: MANAGEMENT
PRADEEP ABOTHU
 
PDF
LAW OF CONTRACT ( 5 YEAR LLB & UNITARY LLB)- MODULE-3 - LEARN THROUGH PICTURE
APARNA T SHAIL KUMAR
 
PPT
Talk on Critical Theory, Part II, Philosophy of Social Sciences
Soraj Hongladarom
 
PDF
Reconstruct, Restore, Reimagine: New Perspectives on Stoke Newington’s Histor...
History of Stoke Newington
 
PDF
The Different Types of Non-Experimental Research
Thelma Villaflores
 
PPTX
Cultivation practice of Litchi in Nepal.pptx
UmeshTimilsina1
 
PPTX
Universal immunization Programme (UIP).pptx
Vishal Chanalia
 
PDF
0725.WHITEPAPER-UNIQUEWAYSOFPROTOTYPINGANDUXNOW.pdf
Thomas GIRARD, MA, CDP
 
PDF
Women's Health: Essential Tips for Every Stage.pdf
Iftikhar Ahmed
 
CONCURSO DE POESIA “POETUFAS – PASSOS SUAVES PELO VERSO.pdf
Colégio Santa Teresinha
 
The dynastic history of the Chahmana.pdf
PrachiSontakke5
 
2025 Winter SWAYAM NPTEL & A Student.pptx
Utsav Yagnik
 
PATIENT ASSIGNMENTS AND NURSING CARE RESPONSIBILITIES.pptx
PRADEEP ABOTHU
 
Stokey: A Jewish Village by Rachel Kolsky
History of Stoke Newington
 
LAW OF CONTRACT (5 YEAR LLB & UNITARY LLB )- MODULE - 1.& 2 - LEARN THROUGH P...
APARNA T SHAIL KUMAR
 
Stereochemistry-Optical Isomerism in organic compoundsptx
Tarannum Nadaf-Mansuri
 
How to Set Maximum Difference Odoo 18 POS
Celine George
 
Generative AI: it's STILL not a robot (CIJ Summer 2025)
Paul Bradshaw
 
Unit 2 COMMERCIAL BANKING, Corporate banking.pptx
AnubalaSuresh1
 
SPINA BIFIDA: NURSING MANAGEMENT .pptx
PRADEEP ABOTHU
 
STAFF DEVELOPMENT AND WELFARE: MANAGEMENT
PRADEEP ABOTHU
 
LAW OF CONTRACT ( 5 YEAR LLB & UNITARY LLB)- MODULE-3 - LEARN THROUGH PICTURE
APARNA T SHAIL KUMAR
 
Talk on Critical Theory, Part II, Philosophy of Social Sciences
Soraj Hongladarom
 
Reconstruct, Restore, Reimagine: New Perspectives on Stoke Newington’s Histor...
History of Stoke Newington
 
The Different Types of Non-Experimental Research
Thelma Villaflores
 
Cultivation practice of Litchi in Nepal.pptx
UmeshTimilsina1
 
Universal immunization Programme (UIP).pptx
Vishal Chanalia
 
0725.WHITEPAPER-UNIQUEWAYSOFPROTOTYPINGANDUXNOW.pdf
Thomas GIRARD, MA, CDP
 
Women's Health: Essential Tips for Every Stage.pdf
Iftikhar Ahmed
 

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