Wardy24
Wardy24

Reputation: 55

Using functions in c#

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

Upvotes: 2

Views: 237

Answers (3)

ΩmegaMan
ΩmegaMan

Reputation: 31616

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.

Upvotes: 1

failedprogramming
failedprogramming

Reputation: 2522

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" .

Upvotes: 3

MasterBettor
MasterBettor

Reputation: 109

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);
 }

Upvotes: 1

Related Questions