Wednesday, September 10, 2008

The Output Parameters

Sometimes when you write a function, you may want it to manipulate and return several values rather than one. Whenever I needed to do this I used to return an object.
But there might be a possibility that the returned object has no other purpose than to be used just to return multiple values from a function.
And then I discovered output parameters. Though I haven't really used them. (Just used them to check the functionality, nothing flashy. :) )

With output parameters you define the variables that will be returned. But you don't use the return keyword. With return keyword you can return a single value or object not multiple.
But with output parameters you don't use the return keyword to return multiple values rather you just specify that these variables are output parameters and their values can be used by the calling program and the called program can manipulate their values (in fact, assign them values).
For the above stated purposes you use the keyword out.

Example Code:
Say, you have a class "Class1.cs" (a very innovative name, right?)
In this class you have a function called ProcessNum which takes two integers as input and as output gives their sum and difference. This is how it will work:

public void ProcessNum(int a, int b, out int Sum, out int Diff)
{
Sum = a + b;
Diff = a - b;
}

Calling ProcessNum:

Class1 obj = new Class1();
int S;
int D;
int num1 = 5;
int num2 = 5;
obj.ProcessNum(num1, num2, out S, out D);
Console.WriteLine("Called ProcessNum of Class1 with parameters " +
"a: " + num1 + Environment.NewLine + " b: " + num2 +
Environment.NewLine + "Returned Values:::: " +
Environment.NewLine + "Sum: " + S + Environment.NewLine +
"Diff: " + D);


Source:

Apress Beginning ASP.Net 2.0 in C# 2005
Chapter # 3: Types, Objects and Namespaces
Pg. #: 75

5 comments:

azimyasin said...

Good hai ! kahan say chapa!

Originally Posted on:
September 16, 2008 10:59 AM

Intricate said...

Tumhein mere capabilities pe itna bhi bharosa nahi hai k mein aik cheez khud kaheen se nikal sakti hoon. Afsoos sad afsoos :P
waisay itna chupanay ke koshish kee thi kaisay pata chala ke kaheen se chappa hai?? :P
Khair was reading a book, yeh baat aisay likhi huai thi jaisay koi normal se baat hai and I was thinking yaar itna time ho gaya par yeh baat ab tak pata kyun nahi thi, hum loog to competitions mein beth ke class banatay thay aik is kaam ke liye, socho..... :) :P

Originally Posted on:
September 16, 2008 7:29 PM

Nazia Gilani said...

Wow...this was so enlightening...didn't know we can do it this way...good good...I wish Bina u had done ur research earlier...maybe we would have won some of the competitions we lost (not that we lost many we participated in):P


Originally Posted on:
September 18, 2008 12:28 AM

Intricate said...

@Nazia.
Yeah right (on the last sentence about not loosing many comps we participated in) :P
And yes you are right, when I found this out, I thought
"Yaar hum kya paglon ke tarah classes banatay thay".
I think we should have gone ahead and read one C# book completely.
Waisay I found this in an ASP.Net Book rather than a C# book.

Originally Posted on:
September 20, 2008 12:28 AM

Nazia Gilani said...

hmn...good good...yaha koi notification type cheez nahi hoti keh pata chale aap keh comment ka reply aya hain?

Originally Posted on:
October 9, 2008 3:32 AM