SlideShare a Scribd company logo
2
Most read
5
Most read
8
Most read
PARAMETER PASSING
Subject guide: Pratibha Sharma
Subject: Advance Java
Name: Khushboo Jethwa
Enrollment no.: 140950107028
Batch: A1
PARAMETERS
A parameter is an intrinsic property of the
procedure, included in its definition.
Parameter passing methods are the ways in
which parameters are transferred between
functions when one function calls another.
Types of parameter
Parameters are of two types :-
• Formal parameters
• Actual parameters
Formal Parameters
Formal parameters are written in the function prototype and
function header of the definition.
Formal parameters are local variables which are assigned values
from the arguments when the function is called.
Example of Formal
Parameters
Return type Function name Formal parameter
float CircleArea (float r) {
const float Pi = 3.1415;
Local object
Definition return Pi * r * r;
}
Return statement Function body
Actual Parameters
 When a function is called, the values
(expressions) that are passed in the call are called
the arguments or actual parameters (both terms
mean the same thing).
 The time of the call each actual parameter is
assigned to the corresponding formal parameter
in the function definition.
Example of Actual Parameter
Actual parameter
cout << CircleArea(MyRadius) << endl
To process the invocation, the function that contains
the insertion statement is suspended and
CircleArea() does its job. The insertion statement is
then completed using the value supplied by
CircleArea().
Parameter Passing
There are four different ways of passing
parameters to a method in C# which are as follows:
•1. Value
•2. Ref (reference)
•3. Out (reference)
•4. Params (parameter arrays)
Passing parameter by value
•By default, parameters are passed by value. In
this method a duplicate copy is made and sent to
the called function. There are two copies of the
variables. So if you change the value in the
called method it won't be changed in the calling
method.
•We use this process when we want to use but
don't want to change the values of the
parameters passed.
example
using System;
namespace value_parameter
{
class Program
{
class XX
{
public int sum(int a, int b)
{
a = a + 10;
b = b + 20;
return (a + b);
}
}
static void Main(string[] args)
{
// local data members have to initialized as they are not initiated with class
constructor
int a = 10, b = 20;
XX obj = new XX();
Console.WriteLine("sum of a and b is : " + obj.sum(a, b));
Console.WriteLine("Value of a is : " + a);
Console.WriteLine("Value of b is : " + b);
Console.ReadLine();
}
}
}
In the above code we changed the values of data
member a and b but it is not reflected back in the
calling method. As the parameters are default
passed by value.
Passing parameter by reference
• Passing parameters by ref uses the address of the actual
parameters to the formal parameters. It requires ref
keyword in front of variables to identify in both actual and
formal parameters.
• The process of ref is bidirectional i.e. we have to supply
value to the formal parameters and we get back processed
value.
• We use this process when we want to use or change the
values of the parameters passed.
example
using System;
namespace ref_parameter
{
class Program
{
class XX
{
public int sum(ref int a, ref int b)
{
a = a + 10;
b = b + 20;
return (a + b);
}
}
static void Main(string[] args)
{
// local data members have to initialized as they are not initiated with
class constructor
int a=10 , b=20 ;
XX obj = new XX();
Console.WriteLine("sum of a and b is : " + obj.sum(ref a, ref b));
Console.WriteLine("Value of a is : " + a);
Console.WriteLine("Value of b is : " + b);
Console.ReadLine();
}
}
}
Passing parameter by out
• Like reference parameters, output parameters don't create
a new storage location and are passed by reference. It
requires out keyword in front of variables to identify in
both actual and formal parameters.
• The process of out is unidirectional i.e. we don't have to
supply value to the formal parameters but we get back
processed value.
• We use this process when we want some parameters to
bring back some processed values form the called method.
example
using System;
namespace out_parameter
{
class Program
{
class XX
{
public int sum(int a, int b, out int c, out int d)
{
c = a + b;
d = c + 100;
return (a + b);
}
}
static void Main(string[] args)
{
// local data members have to initialized as they are not initiated with class
constructor
int a = 10, b = 20;
// the out data members doesn't need to assign initial value as they are
processed and brought back
int c, d;
XX obj = new XX();
Console.WriteLine("sum of a and b is : " + obj.sum(a, b, out c, outd));
Console.WriteLine("Value of a is : " + a);
Console.WriteLine("Value of b is : " + b);
Console.WriteLine("Value of c is : " + c);
Console.WriteLine("Value of d is : " + d);
Console.ReadLine();
}
}
}
Passing parameter by param
(parameter arrays)
• Params parameters "params" parameter is a very useful
feature in C#. It is used when we don't know the number
of parameters will be passed to the called method. Param
can accept multiple values or "params" should be a single
dimensional or a jagged array.
• The params keyword lets you specify a method parameter
that takes an argument where the number of arguments is
variable.
• No additional parameters are permitted after the params
keyword in a method declaration, and only one params
keyword is permitted in a method declaration.
example
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace param_parameter
{
class Program
{
class XX
{
public void print(params int[] numbers)
{
foreach(int x in numbers)
{
Console.WriteLine(" " + x);
}
}
}
static void Main(string[] args)
{
int[] numbers = { 1, 2, 3, 4, 5, 6 };
int a = 10, b = 20, c = 30, d = 40;
XX obj = new XX();
obj.print(a, b, c, d);
obj.print(numbers);
Console.ReadLine();
}
}
}
THANK YOU !!

More Related Content

What's hot (20)

PPTX
array of object pointer in c++
Arpita Patel
 
PPS
Introduction to class in java
kamal kotecha
 
PPTX
Control structures in c++
Nitin Jawla
 
PPTX
arrays and pointers
Samiksha Pun
 
PPT
Operator Overloading
Nilesh Dalvi
 
PPSX
Data Types & Variables in JAVA
Ankita Totala
 
PPTX
Variables and Data Types
Infoviaan Technologies
 
PPT
9. Input Output in java
Nilesh Dalvi
 
PPTX
Constructors and Destructor in C++
International Institute of Information Technology (I²IT)
 
PPT
Operator overloading in C++
BalajiGovindan5
 
PPTX
Exception Handling in object oriented programming using C++
Janki Shah
 
PDF
Exception handling
Pranali Chaudhari
 
PPT
Control structure C++
Anil Kumar
 
PPT
16 virtual function
Docent Education
 
PPTX
Templates in C++
Tech_MX
 
PPTX
Type Conversion, Precedence and Associativity
Aakash Singh
 
PPTX
classes and objects in C++
HalaiHansaika
 
PPTX
Control Statements in Java
Niloy Saha
 
PPTX
[OOP - Lec 19] Static Member Functions
Muhammad Hammad Waseem
 
PPTX
Control statements in c
Sathish Narayanan
 
array of object pointer in c++
Arpita Patel
 
Introduction to class in java
kamal kotecha
 
Control structures in c++
Nitin Jawla
 
arrays and pointers
Samiksha Pun
 
Operator Overloading
Nilesh Dalvi
 
Data Types & Variables in JAVA
Ankita Totala
 
Variables and Data Types
Infoviaan Technologies
 
9. Input Output in java
Nilesh Dalvi
 
Operator overloading in C++
BalajiGovindan5
 
Exception Handling in object oriented programming using C++
Janki Shah
 
Exception handling
Pranali Chaudhari
 
Control structure C++
Anil Kumar
 
16 virtual function
Docent Education
 
Templates in C++
Tech_MX
 
Type Conversion, Precedence and Associativity
Aakash Singh
 
classes and objects in C++
HalaiHansaika
 
Control Statements in Java
Niloy Saha
 
[OOP - Lec 19] Static Member Functions
Muhammad Hammad Waseem
 
Control statements in c
Sathish Narayanan
 

Similar to parameter passing in c# (20)

PPTX
Method parameters in C# - All types of parameter passing in C #
JayanthiM19
 
PDF
Xamarin: C# Methods
Eng Teong Cheah
 
PPTX
Module 4 : methods & parameters
Prem Kumar Badri
 
PDF
csharp_Passing_parameters_by_value_and_reference
Micheal Ogundero
 
PPTX
Method parameters in c#
Dr.Neeraj Kumar Pandey
 
PPT
Methods in C#
Prasanna Kumar SM
 
PPTX
Intro to C# - part 2.pptx emerging technology
worldchannel
 
PPTX
14method in c#
Sireesh K
 
PPT
Visula C# Programming Lecture 6
Abou Bakr Ashraf
 
PPT
Csphtp1 06
HUST
 
PPTX
Methods In C-Sharp (C#)
Abid Kohistani
 
PPTX
Class 10
SIVASHANKARIRAJAN
 
PPTX
METHODS OR FUNCTIONS IN C for dotnet.pptx
ArjunKhanal8
 
PDF
static methods
Micheal Ogundero
 
PPTX
Parameter passing to_functions_in_c
ForwardBlog Enewzletter
 
PPTX
Reference Parameter, Passing object by reference, constant parameter & Defaul...
Meghaj Mallick
 
PPTX
Methods
Bhushan Mulmule
 
PPTX
Chap2 class,objects contd
raksharao
 
PPT
Cso gaddis java_chapter5
mlrbrown
 
PPTX
EContent_11_2023_04_09_11_30_38_Unit_3_Objects_and_Classespptx__2023_03_20_12...
vekariyakashyap
 
Method parameters in C# - All types of parameter passing in C #
JayanthiM19
 
Xamarin: C# Methods
Eng Teong Cheah
 
Module 4 : methods & parameters
Prem Kumar Badri
 
csharp_Passing_parameters_by_value_and_reference
Micheal Ogundero
 
Method parameters in c#
Dr.Neeraj Kumar Pandey
 
Methods in C#
Prasanna Kumar SM
 
Intro to C# - part 2.pptx emerging technology
worldchannel
 
14method in c#
Sireesh K
 
Visula C# Programming Lecture 6
Abou Bakr Ashraf
 
Csphtp1 06
HUST
 
Methods In C-Sharp (C#)
Abid Kohistani
 
METHODS OR FUNCTIONS IN C for dotnet.pptx
ArjunKhanal8
 
static methods
Micheal Ogundero
 
Parameter passing to_functions_in_c
ForwardBlog Enewzletter
 
Reference Parameter, Passing object by reference, constant parameter & Defaul...
Meghaj Mallick
 
Chap2 class,objects contd
raksharao
 
Cso gaddis java_chapter5
mlrbrown
 
EContent_11_2023_04_09_11_30_38_Unit_3_Objects_and_Classespptx__2023_03_20_12...
vekariyakashyap
 
Ad

More from khush_boo31 (6)

PPTX
L attribute in compiler design
khush_boo31
 
PPTX
Classification of data mart
khush_boo31
 
PPT
statement interface
khush_boo31
 
PPT
context free language
khush_boo31
 
PPT
Knapsack problem using dynamic programming
khush_boo31
 
PPT
Parsing
khush_boo31
 
L attribute in compiler design
khush_boo31
 
Classification of data mart
khush_boo31
 
statement interface
khush_boo31
 
context free language
khush_boo31
 
Knapsack problem using dynamic programming
khush_boo31
 
Parsing
khush_boo31
 
Ad

Recently uploaded (20)

PPTX
How to Set Maximum Difference Odoo 18 POS
Celine George
 
DOCX
A summary of SPRING SILKWORMS by Mao Dun.docx
maryjosie1
 
PDF
People & Earth's Ecosystem -Lesson 2: People & Population
marvinnbustamante1
 
PPTX
How to Configure Prepayments in Odoo 18 Sales
Celine George
 
PPTX
ASRB NET 2023 PREVIOUS YEAR QUESTION PAPER GENETICS AND PLANT BREEDING BY SAT...
Krashi Coaching
 
PPTX
HYDROCEPHALUS: NURSING MANAGEMENT .pptx
PRADEEP ABOTHU
 
PPTX
STAFF DEVELOPMENT AND WELFARE: MANAGEMENT
PRADEEP ABOTHU
 
PDF
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 
PPTX
How to Configure Access Rights of Manufacturing Orders in Odoo 18 Manufacturing
Celine George
 
PDF
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 - GLOBAL SUCCESS - CẢ NĂM - NĂM 2024 (VOCABULARY, ...
Nguyen Thanh Tu Collection
 
PDF
Federal dollars withheld by district, charter, grant recipient
Mebane Rash
 
PPT
Talk on Critical Theory, Part One, Philosophy of Social Sciences
Soraj Hongladarom
 
PPTX
Soil and agriculture microbiology .pptx
Keerthana Ramesh
 
PPTX
Pyhton with Mysql to perform CRUD operations.pptx
Ramakrishna Reddy Bijjam
 
PDF
IMP NAAC REFORMS 2024 - 10 Attributes.pdf
BHARTIWADEKAR
 
PDF
CHILD RIGHTS AND PROTECTION QUESTION BANK
Dr Raja Mohammed T
 
PDF
LAW OF CONTRACT ( 5 YEAR LLB & UNITARY LLB)- MODULE-3 - LEARN THROUGH PICTURE
APARNA T SHAIL KUMAR
 
PPTX
Quarter1-English3-W4-Identifying Elements of the Story
FLORRACHELSANTOS
 
PDF
LAW OF CONTRACT (5 YEAR LLB & UNITARY LLB )- MODULE - 1.& 2 - LEARN THROUGH P...
APARNA T SHAIL KUMAR
 
PPTX
How to Manage Access Rights & User Types in Odoo 18
Celine George
 
How to Set Maximum Difference Odoo 18 POS
Celine George
 
A summary of SPRING SILKWORMS by Mao Dun.docx
maryjosie1
 
People & Earth's Ecosystem -Lesson 2: People & Population
marvinnbustamante1
 
How to Configure Prepayments in Odoo 18 Sales
Celine George
 
ASRB NET 2023 PREVIOUS YEAR QUESTION PAPER GENETICS AND PLANT BREEDING BY SAT...
Krashi Coaching
 
HYDROCEPHALUS: NURSING MANAGEMENT .pptx
PRADEEP ABOTHU
 
STAFF DEVELOPMENT AND WELFARE: MANAGEMENT
PRADEEP ABOTHU
 
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 
How to Configure Access Rights of Manufacturing Orders in Odoo 18 Manufacturing
Celine George
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 - GLOBAL SUCCESS - CẢ NĂM - NĂM 2024 (VOCABULARY, ...
Nguyen Thanh Tu Collection
 
Federal dollars withheld by district, charter, grant recipient
Mebane Rash
 
Talk on Critical Theory, Part One, Philosophy of Social Sciences
Soraj Hongladarom
 
Soil and agriculture microbiology .pptx
Keerthana Ramesh
 
Pyhton with Mysql to perform CRUD operations.pptx
Ramakrishna Reddy Bijjam
 
IMP NAAC REFORMS 2024 - 10 Attributes.pdf
BHARTIWADEKAR
 
CHILD RIGHTS AND PROTECTION QUESTION BANK
Dr Raja Mohammed T
 
LAW OF CONTRACT ( 5 YEAR LLB & UNITARY LLB)- MODULE-3 - LEARN THROUGH PICTURE
APARNA T SHAIL KUMAR
 
Quarter1-English3-W4-Identifying Elements of the Story
FLORRACHELSANTOS
 
LAW OF CONTRACT (5 YEAR LLB & UNITARY LLB )- MODULE - 1.& 2 - LEARN THROUGH P...
APARNA T SHAIL KUMAR
 
How to Manage Access Rights & User Types in Odoo 18
Celine George
 

parameter passing in c#

  • 1. PARAMETER PASSING Subject guide: Pratibha Sharma Subject: Advance Java Name: Khushboo Jethwa Enrollment no.: 140950107028 Batch: A1
  • 2. PARAMETERS A parameter is an intrinsic property of the procedure, included in its definition. Parameter passing methods are the ways in which parameters are transferred between functions when one function calls another.
  • 3. Types of parameter Parameters are of two types :- • Formal parameters • Actual parameters
  • 4. Formal Parameters Formal parameters are written in the function prototype and function header of the definition. Formal parameters are local variables which are assigned values from the arguments when the function is called.
  • 5. Example of Formal Parameters Return type Function name Formal parameter float CircleArea (float r) { const float Pi = 3.1415; Local object Definition return Pi * r * r; } Return statement Function body
  • 6. Actual Parameters  When a function is called, the values (expressions) that are passed in the call are called the arguments or actual parameters (both terms mean the same thing).  The time of the call each actual parameter is assigned to the corresponding formal parameter in the function definition.
  • 7. Example of Actual Parameter Actual parameter cout << CircleArea(MyRadius) << endl To process the invocation, the function that contains the insertion statement is suspended and CircleArea() does its job. The insertion statement is then completed using the value supplied by CircleArea().
  • 8. Parameter Passing There are four different ways of passing parameters to a method in C# which are as follows: •1. Value •2. Ref (reference) •3. Out (reference) •4. Params (parameter arrays)
  • 9. Passing parameter by value •By default, parameters are passed by value. In this method a duplicate copy is made and sent to the called function. There are two copies of the variables. So if you change the value in the called method it won't be changed in the calling method. •We use this process when we want to use but don't want to change the values of the parameters passed.
  • 10. example using System; namespace value_parameter { class Program { class XX { public int sum(int a, int b) { a = a + 10; b = b + 20; return (a + b); } } static void Main(string[] args)
  • 11. { // local data members have to initialized as they are not initiated with class constructor int a = 10, b = 20; XX obj = new XX(); Console.WriteLine("sum of a and b is : " + obj.sum(a, b)); Console.WriteLine("Value of a is : " + a); Console.WriteLine("Value of b is : " + b); Console.ReadLine(); } } }
  • 12. In the above code we changed the values of data member a and b but it is not reflected back in the calling method. As the parameters are default passed by value.
  • 13. Passing parameter by reference • Passing parameters by ref uses the address of the actual parameters to the formal parameters. It requires ref keyword in front of variables to identify in both actual and formal parameters. • The process of ref is bidirectional i.e. we have to supply value to the formal parameters and we get back processed value. • We use this process when we want to use or change the values of the parameters passed.
  • 14. example using System; namespace ref_parameter { class Program { class XX { public int sum(ref int a, ref int b) { a = a + 10; b = b + 20; return (a + b); } } static void Main(string[] args)
  • 15. { // local data members have to initialized as they are not initiated with class constructor int a=10 , b=20 ; XX obj = new XX(); Console.WriteLine("sum of a and b is : " + obj.sum(ref a, ref b)); Console.WriteLine("Value of a is : " + a); Console.WriteLine("Value of b is : " + b); Console.ReadLine(); } } }
  • 16. Passing parameter by out • Like reference parameters, output parameters don't create a new storage location and are passed by reference. It requires out keyword in front of variables to identify in both actual and formal parameters. • The process of out is unidirectional i.e. we don't have to supply value to the formal parameters but we get back processed value. • We use this process when we want some parameters to bring back some processed values form the called method.
  • 17. example using System; namespace out_parameter { class Program { class XX { public int sum(int a, int b, out int c, out int d) { c = a + b; d = c + 100; return (a + b); } } static void Main(string[] args) {
  • 18. // local data members have to initialized as they are not initiated with class constructor int a = 10, b = 20; // the out data members doesn't need to assign initial value as they are processed and brought back int c, d; XX obj = new XX(); Console.WriteLine("sum of a and b is : " + obj.sum(a, b, out c, outd)); Console.WriteLine("Value of a is : " + a); Console.WriteLine("Value of b is : " + b); Console.WriteLine("Value of c is : " + c); Console.WriteLine("Value of d is : " + d); Console.ReadLine(); } } }
  • 19. Passing parameter by param (parameter arrays) • Params parameters "params" parameter is a very useful feature in C#. It is used when we don't know the number of parameters will be passed to the called method. Param can accept multiple values or "params" should be a single dimensional or a jagged array. • The params keyword lets you specify a method parameter that takes an argument where the number of arguments is variable. • No additional parameters are permitted after the params keyword in a method declaration, and only one params keyword is permitted in a method declaration.
  • 20. example using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace param_parameter { class Program { class XX { public void print(params int[] numbers) { foreach(int x in numbers) { Console.WriteLine(" " + x); } } } static void Main(string[] args) {
  • 21. int[] numbers = { 1, 2, 3, 4, 5, 6 }; int a = 10, b = 20, c = 30, d = 40; XX obj = new XX(); obj.print(a, b, c, d); obj.print(numbers); Console.ReadLine(); } } }