03.01.2015 Views

Complete set: Intro to C - Bill Buchanan

Complete set: Intro to C - Bill Buchanan

Complete set: Intro to C - Bill Buchanan

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

}<br />

x *= x;<br />

System.Console.WriteLine("Value in the method is {0}",x);<br />

return(x);<br />

static void Main(string[] args)<br />

{<br />

double val1,val2;<br />

val1=10;<br />

}<br />

}<br />

}<br />

val2=findSquare(val1);<br />

System.Console.WriteLine("Value now is {0}",val1);<br />

System.Console.ReadLine();<br />

Sample Run Error! Reference source not found.<br />

Value in the method is 100<br />

Value now is 10<br />

4.2.2 Passing by reference<br />

It is possible <strong>to</strong> pass a reference of a parameter, so that the parameter is changed by<br />

the method. For this the ref keyword is inserted before the variable that is passed. It<br />

can be see from the test run of Program 4.2 that the actual value of the parameter<br />

can now be changed by the method.<br />

Program 4.2: Program4_2_PassingByReference<br />

using System;<br />

namespace ConsoleApplication2<br />

{<br />

class Class1<br />

{<br />

duction <strong>to</strong> .NET<br />

<strong>Intro</strong><br />

static double findSquare(ref double x)<br />

{<br />

x *= x;<br />

System.Console.WriteLine("Value in the method is {0}",x);<br />

return(x);<br />

}<br />

static void Main(string[] args)<br />

{<br />

double val1,val2;<br />

}<br />

val1=10;<br />

val2=findSquare(ref val1);<br />

System.Console.WriteLine("Value now is {0}",val1);<br />

System.Console.ReadLine();<br />

Agilent .NET Course: Type and Debugging 3

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!