SlideShare a Scribd company logo
1 of 22
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

What's hot (20)

C# classes objects
C#  classes objectsC#  classes objects
C# classes objects
 
Data Structures- Part5 recursion
Data Structures- Part5 recursionData Structures- Part5 recursion
Data Structures- Part5 recursion
 
Java abstract class & abstract methods
Java abstract class & abstract methodsJava abstract class & abstract methods
Java abstract class & abstract methods
 
Data types in c++
Data types in c++Data types in c++
Data types in c++
 
Type casting in java
Type casting in javaType casting in java
Type casting in java
 
Classes and objects
Classes and objectsClasses and objects
Classes and objects
 
Constructor and destructor
Constructor  and  destructor Constructor  and  destructor
Constructor and destructor
 
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
Chapter 02: Classes Objects and Methods Java by Tushar B KuteChapter 02: Classes Objects and Methods Java by Tushar B Kute
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
 
Performance analysis(Time & Space Complexity)
Performance analysis(Time & Space Complexity)Performance analysis(Time & Space Complexity)
Performance analysis(Time & Space Complexity)
 
Templates in C++
Templates in C++Templates in C++
Templates in C++
 
Data types in php
Data types in phpData types in php
Data types in php
 
Java(Polymorphism)
Java(Polymorphism)Java(Polymorphism)
Java(Polymorphism)
 
Java static keyword
Java static keywordJava static keyword
Java static keyword
 
Chapter 7 - Input Output Statements in C++
Chapter 7 - Input Output Statements in C++Chapter 7 - Input Output Statements in C++
Chapter 7 - Input Output Statements in C++
 
Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++
 
This and Static Keyword
This and Static KeywordThis and Static Keyword
This and Static Keyword
 
OOPS Basics With Example
OOPS Basics With ExampleOOPS Basics With Example
OOPS Basics With Example
 
Arrays
ArraysArrays
Arrays
 
Methods in C#
Methods in C#Methods in C#
Methods in C#
 
Java Data Types
Java Data TypesJava Data Types
Java Data Types
 

Similar to parameter passing in c#

16717 functions in C++
16717 functions in C++16717 functions in C++
16717 functions in C++LPU
 
Classes function overloading
Classes function overloadingClasses function overloading
Classes function overloadingankush_kumar
 
Reference Parameter, Passing object by reference, constant parameter & Defaul...
Reference Parameter, Passing object by reference, constant parameter & Defaul...Reference Parameter, Passing object by reference, constant parameter & Defaul...
Reference Parameter, Passing object by reference, constant parameter & Defaul...Meghaj Mallick
 
CH.4FUNCTIONS IN C (1).pptx
CH.4FUNCTIONS IN C (1).pptxCH.4FUNCTIONS IN C (1).pptx
CH.4FUNCTIONS IN C (1).pptxsangeeta borde
 
EST 102 Programming in C-MODULE 4
EST 102 Programming in C-MODULE 4EST 102 Programming in C-MODULE 4
EST 102 Programming in C-MODULE 4NIMMYRAJU
 
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
 
Functions and pointers_unit_4
Functions and pointers_unit_4Functions and pointers_unit_4
Functions and pointers_unit_4Saranya saran
 
Operator_Overloaing_Type_Conversion_OOPC(C++)
Operator_Overloaing_Type_Conversion_OOPC(C++)Operator_Overloaing_Type_Conversion_OOPC(C++)
Operator_Overloaing_Type_Conversion_OOPC(C++)Yaksh Jethva
 
eee2-day4-structures engineering college
eee2-day4-structures engineering collegeeee2-day4-structures engineering college
eee2-day4-structures engineering college2017eee0459
 

Similar to parameter passing in c# (20)

16717 functions in C++
16717 functions in C++16717 functions in C++
16717 functions in C++
 
Functions
FunctionsFunctions
Functions
 
Classes function overloading
Classes function overloadingClasses function overloading
Classes function overloading
 
unit_2.pptx
unit_2.pptxunit_2.pptx
unit_2.pptx
 
4th unit full
4th unit full4th unit full
4th unit full
 
Reference Parameter, Passing object by reference, constant parameter & Defaul...
Reference Parameter, Passing object by reference, constant parameter & Defaul...Reference Parameter, Passing object by reference, constant parameter & Defaul...
Reference Parameter, Passing object by reference, constant parameter & Defaul...
 
CH.4FUNCTIONS IN C (1).pptx
CH.4FUNCTIONS IN C (1).pptxCH.4FUNCTIONS IN C (1).pptx
CH.4FUNCTIONS IN C (1).pptx
 
EST 102 Programming in C-MODULE 4
EST 102 Programming in C-MODULE 4EST 102 Programming in C-MODULE 4
EST 102 Programming in C-MODULE 4
 
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...
 
Functions in C++.pdf
Functions in C++.pdfFunctions in C++.pdf
Functions in C++.pdf
 
Chapter 5
Chapter 5Chapter 5
Chapter 5
 
PSPC-UNIT-4.pdf
PSPC-UNIT-4.pdfPSPC-UNIT-4.pdf
PSPC-UNIT-4.pdf
 
3 Function & Storage Class.pptx
3 Function & Storage Class.pptx3 Function & Storage Class.pptx
3 Function & Storage Class.pptx
 
Functions and pointers_unit_4
Functions and pointers_unit_4Functions and pointers_unit_4
Functions and pointers_unit_4
 
Ch06
Ch06Ch06
Ch06
 
Operator_Overloaing_Type_Conversion_OOPC(C++)
Operator_Overloaing_Type_Conversion_OOPC(C++)Operator_Overloaing_Type_Conversion_OOPC(C++)
Operator_Overloaing_Type_Conversion_OOPC(C++)
 
unit_2 (1).pptx
unit_2 (1).pptxunit_2 (1).pptx
unit_2 (1).pptx
 
eee2-day4-structures engineering college
eee2-day4-structures engineering collegeeee2-day4-structures engineering college
eee2-day4-structures engineering college
 
Unit iv functions
Unit  iv functionsUnit  iv functions
Unit iv functions
 
Python_Functions_Unit1.pptx
Python_Functions_Unit1.pptxPython_Functions_Unit1.pptx
Python_Functions_Unit1.pptx
 

More from khush_boo31

L attribute in compiler design
L  attribute in compiler designL  attribute in compiler design
L attribute in compiler designkhush_boo31
 
Classification of data mart
Classification of data martClassification of data mart
Classification of data martkhush_boo31
 
statement interface
statement interface statement interface
statement interface khush_boo31
 
context free language
context free languagecontext free language
context free languagekhush_boo31
 
Knapsack problem using dynamic programming
Knapsack problem using dynamic programmingKnapsack problem using dynamic programming
Knapsack problem using dynamic programmingkhush_boo31
 

More from khush_boo31 (6)

L attribute in compiler design
L  attribute in compiler designL  attribute in compiler design
L attribute in compiler design
 
Classification of data mart
Classification of data martClassification of data mart
Classification of data mart
 
statement interface
statement interface statement interface
statement interface
 
context free language
context free languagecontext free language
context free language
 
Knapsack problem using dynamic programming
Knapsack problem using dynamic programmingKnapsack problem using dynamic programming
Knapsack problem using dynamic programming
 
Parsing
ParsingParsing
Parsing
 

Recently uploaded

AIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.pptAIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.pptNishitharanjan Rout
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jisc
 
How to Add a Tool Tip to a Field in Odoo 17
How to Add a Tool Tip to a Field in Odoo 17How to Add a Tool Tip to a Field in Odoo 17
How to Add a Tool Tip to a Field in Odoo 17Celine George
 
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxOn_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxPooja Bhuva
 
What is 3 Way Matching Process in Odoo 17.pptx
What is 3 Way Matching Process in Odoo 17.pptxWhat is 3 Way Matching Process in Odoo 17.pptx
What is 3 Way Matching Process in Odoo 17.pptxCeline George
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...Nguyen Thanh Tu Collection
 
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSSpellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSAnaAcapella
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxJisc
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - Englishneillewis46
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Pooja Bhuva
 
How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17Celine George
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Jisc
 
Play hard learn harder: The Serious Business of Play
Play hard learn harder:  The Serious Business of PlayPlay hard learn harder:  The Serious Business of Play
Play hard learn harder: The Serious Business of PlayPooky Knightsmith
 
Tatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf artsTatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf artsNbelano25
 
Introduction to TechSoup’s Digital Marketing Services and Use Cases
Introduction to TechSoup’s Digital Marketing  Services and Use CasesIntroduction to TechSoup’s Digital Marketing  Services and Use Cases
Introduction to TechSoup’s Digital Marketing Services and Use CasesTechSoup
 
Model Attribute _rec_name in the Odoo 17
Model Attribute _rec_name in the Odoo 17Model Attribute _rec_name in the Odoo 17
Model Attribute _rec_name in the Odoo 17Celine George
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Pooja Bhuva
 
Details on CBSE Compartment Exam.pptx1111
Details on CBSE Compartment Exam.pptx1111Details on CBSE Compartment Exam.pptx1111
Details on CBSE Compartment Exam.pptx1111GangaMaiya1
 

Recently uploaded (20)

AIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.pptAIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.ppt
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
How to Add a Tool Tip to a Field in Odoo 17
How to Add a Tool Tip to a Field in Odoo 17How to Add a Tool Tip to a Field in Odoo 17
How to Add a Tool Tip to a Field in Odoo 17
 
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxOn_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
 
What is 3 Way Matching Process in Odoo 17.pptx
What is 3 Way Matching Process in Odoo 17.pptxWhat is 3 Way Matching Process in Odoo 17.pptx
What is 3 Way Matching Process in Odoo 17.pptx
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
 
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSSpellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
Play hard learn harder: The Serious Business of Play
Play hard learn harder:  The Serious Business of PlayPlay hard learn harder:  The Serious Business of Play
Play hard learn harder: The Serious Business of Play
 
Our Environment Class 10 Science Notes pdf
Our Environment Class 10 Science Notes pdfOur Environment Class 10 Science Notes pdf
Our Environment Class 10 Science Notes pdf
 
Tatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf artsTatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf arts
 
Introduction to TechSoup’s Digital Marketing Services and Use Cases
Introduction to TechSoup’s Digital Marketing  Services and Use CasesIntroduction to TechSoup’s Digital Marketing  Services and Use Cases
Introduction to TechSoup’s Digital Marketing Services and Use Cases
 
OS-operating systems- ch05 (CPU Scheduling) ...
OS-operating systems- ch05 (CPU Scheduling) ...OS-operating systems- ch05 (CPU Scheduling) ...
OS-operating systems- ch05 (CPU Scheduling) ...
 
Model Attribute _rec_name in the Odoo 17
Model Attribute _rec_name in the Odoo 17Model Attribute _rec_name in the Odoo 17
Model Attribute _rec_name in the Odoo 17
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 
Details on CBSE Compartment Exam.pptx1111
Details on CBSE Compartment Exam.pptx1111Details on CBSE Compartment Exam.pptx1111
Details on CBSE Compartment Exam.pptx1111
 

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(); } } }