2

I am struggling to understand how functions work outside of main. I simply need to compute a total using information that is put in by the user in main, but I am supposed to call on a function to total this up. Here is the code I have so far, I am sure it is not very close to right, a nudge in the right direction would be a huge help

namespace ConsoleApplication17
{
class Program
{
    static void Main(string[] args)
    {
        string customerName, customerState;
        double numberItems, itemPrice;
        Console.WriteLine("Please enter the customer name");
        customerName = Console.ReadLine();
        Console.WriteLine("Please enter the state in which you reside:");
        customerState = Console.ReadLine();
        Console.WriteLine("How many items were purchased?");
        numberItems = int.Parse(Console.ReadLine());
        Console.WriteLine("What was the price of said items?");
        itemPrice = Convert.ToDouble(Console.ReadLine());

    }
    public static double ComputeTotal (double total)
    {
        double totalAmount;
        Console.Write(total);
}

}

public static double ComputeTax (double totalAmount, string state, string fl, string nj, string ny)
    {
    if (state == fl)
        return totalAmount*.06;
    else if (state == nj)
        return totalAmount*.07;
    else if (state == ny)
        return totalAmount*.04;
    }

In short, I need to use the function ComputeTotal to multiply the numberItems and itemPrice

3
  • Why not just call ComputeTotal()? Commented Feb 24, 2015 at 2:34
  • @GrantWinney So here is what I was trying to say without being too wordy. This is for class and my teacher went over it quick, and I do not understand how to pass those to the method. I've been looking at examples but with no decent explanation it is pretty tough to just get on my own. Commented Feb 24, 2015 at 2:36
  • 1
    Thanks, that actually helps quite a bit. Was not looking for a straight up answer, just a better grasp of the subject. Commented Feb 24, 2015 at 2:40

3 Answers 3

3

A function basically takes some data from you and returns (some other?) data to you. In your case you want to give the function 2 pieces of data - qty and price, and you want it to return you the total price.

public static double ComputeTotal(double qty, double price)
{
     return qty* price;
}

This function ComputeTotal accepts 2 variables of type double. They are qty and price. The function then multiplies these 2 variables and returns the result to the caller.

In your main method, this is how you use(call) the function.

static void Main(string[] args)
{
   // rest of your code here


   var total = ComputeTotal(numberItems, itemPrice);

   Console.WriteLine(total);

}

Here I am creating a new variable called total, and I am assigning the return value of ComputeTotal function to this variable.

The ComputeTotal function requires 2 parameters and I am passing 2 variables that you created in your code. For brevity I have not typed any of your original code, and your code should be at the location of my comment "// rest of your code here" .

Sign up to request clarification or add additional context in comments.

10 Comments

Not sure if it is outside the scope of your question, but your program will blow up if ppl don't type a decimal number. e.g. ABC instead of 1.25
Alright so I am getting an error, saying no overload for method ComputeTotal takes 0 arguments. I am quite new to this, is there a certain way I should be calling it in the main?
if you look at my second code block at "var total = ComputeTotal(numberItems, itemPrice);" Did you do the same in your code and pass those 2 variables? i.e. the stuff in the brackets.
Okay so just to make sure I actually get it and I don't just copy paste, when I use a function, I have to label it that way. As in when I am calling on variables from main, I have to label them when declaring the function. And when calling the function, I have to declare the variables that I am using? Variables being numberItems and itemPrice
Sorry I should have used different names in my ComputeTotal function to make it less confusing. Please see my edited answer. As you can see, the function does not need to share the names of your variables. They can be something totally different. However when you call the function in your Main method, you will need to use the names of your variables, so that it knows which variables to pass to ComputeTotal.
|
1
your method/function could be like this
   public static double ComputeTotal (double itemPrice, int quantity)
   {
      return  itemPrice * quantity
   }

in your main method you can do like this

  static void Main(string[] args)
  {
    double total_price=0.0;
    total_price = ComputeTotal ( itemPrice, numberItems)
     Console.WriteLine("totl price : {0}",total_price);
 }

Comments

1

understand how functions work

I am distilling this significantly, but a function for this definition is really a method which returns a value. In C# there is no distinction between functions and methods for they are the same with differences being whether something returns data or operates on a referenced instance of a class instance.

The real difference is in the calling mechanism on whether one instantiates (calls new on a class); they are instantiatitng a class. For this assignment, your teacher does not want you to instantiate a class.

Hence you will call function(s) which are static methods that can be called by anyone without instantiating any classes.

With that in mind, your teacher wants you to learn a function type call so he /she wants you to create a static method off of the class Program which can be called by Main because it is static as well.

So create your function type static method that will return a value; hence it will mimic functions in other languages.

outside of main.

Now Main can have static methods, but so can other classes which can be called from within a Main's static method as well.

The other class like that looks like this...and is called by fully qualifying the location such as {ClassName}.{Static Method Name}.

class Program { 

static void Main(...)
{
  Console.WriteLine( TheOtherClass.AFunction() );
}
}

public class TheOtherClass
{
   public static string AFunction()
   {
      return "A String From this Function. :-) ";
   }
}

Note if TheOtherClass is in a different namespace, access it such as {Namespace}.{ClassName}.{Static Method Name}. But you should make sure that the other class is in the same Namespace as found in your current example of ConsoleApplication17.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.