Solved

# Call a function

Posted on 2005-03-15
Medium Priority
169 Views
I'm novice in C#. Here is a basic question --
I have 4 variables a, b, c, d in class main(). I want to perform all the mathematical operations on these variables in a function called maths() and get the values back to main. How can I achieve this?

0
Question by:praveenuni
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points

LVL 9

Accepted Solution

tinchos earned 600 total points
ID: 13547348
Hi

try with

void MyMethod( ref int a, ref int b, ref int c, ref int d )
{
a = 1;
b = 1;
c = 1;
d = 1;
}

int main1()
{
int a = 0;
int b = 0;
int c = 0;
int d = 0;

// Code

MyMethod( ref a, ref b, ref c, ref d );

return 0;
}

Hope this helps

Tincho
0

LVL 20

Assisted Solution

Daniel Van Der Werken earned 400 total points
ID: 13547417
This compiles and runs in Visual Studio .NET 2003:

using System;

namespace maths
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
static double maths(double a, double b, double c, double d)
{
double total = a + b + c + d;
}
/// <summary>
/// The main entry point for the application.
/// </summary>
static void Main(string[] args)
{
double a=1, b=2, c=3, d=4;

//
// TODO: Add code to start application here
//
}
}
}
0

Author Comment

ID: 13547573
Thanks for the replies .. one thing I forgot to tell is .. I want to get all the 4 variables to my main function. Becuase in maths function their values will change and I will be getting a,b,c,d with new values.

-- Praveen
0

LVL 5

Expert Comment

ID: 13547649
hi Praveen,

what tinchos has mentioned is the right way to do what you want to accomplish. To add to that...you may use either of 'out' or 'ref' method parameters. e.g.

public static Main()
{
int a,b,c,d;
int result1 = Maths(out a,out b, out c, out d);
int result2 = a+b+c+d;
}

private int Maths(out int a, out int b, out int c,out int d)
{
a = 2;
b = 3;
c = 4;
d = 5;
return (a+b+c+d);
}
//result1 and result2 will have same value = 14

the difference between 'out' and 'ref' is that, you have initialize 'ref' variables before passing, while 'out' variables must be assigned some value before returning from the function.
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
###### Suggested Courses
Course of the Month13 days, 3 hours left to enroll