?
Solved

Call a function

Posted on 2005-03-15
4
Medium Priority
?
169 Views
Last Modified: 2010-04-16
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
Comment
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
  • Learn & ask questions
4 Comments
 
LVL 9

Accepted Solution

by:
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

by:Daniel Van Der Werken
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;
                  return total;
            }
            /// <summary>
            /// The main entry point for the application.
            /// </summary>
            [STAThread]
            static void Main(string[] args)
            {
                  double a=1, b=2, c=3, d=4;
                  double answer=0;
                  answer=maths(a,b,c,d);
                  Console.WriteLine("Answer is {0}", answer);

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

Author Comment

by:praveenuni
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

by:RoninThe
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question