Reputation: 29
My goal is to define the functions and then call upon them as I need. I am modeling my code off of similar questions asked on here. But I never actually define the terms in the the first chunk of code and then I can't figure out how to call upon them in the second portion. I have exhausted the resources in my book and online.
If anyone could explain to me a bit more clearly how to call upon the other methods, it would be greatly appreciated.
Here is my code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FunctionCalls
{
class Functions
{
static public double addNumbers(double number1, double number2)
{
double result1 = number1 + number2;
return result1;
}
static public double subtractNumbers(double number1, double number2)
{
double result2 = number1 - number2;
return result2;
}
static public double avgNumbers(double number1, double number2)
{
double result3 = (number1 + number2) / 2;
return result3;
}
public static void Main(String[] args)
{
Functions.addNumbers(result1);
Functions.subNumbers(result2);
Functions.avgNumbers(result3);
Console.WriteLine($"The sum of your numbers is {0}", result1);
Console.WriteLine($"The difference of your numbers is {0}", result2);
Console.WriteLine($"The average of your numbers is {0}", result3);
Console.ReadKey();
}
}
Upvotes: 2
Views: 118
Reputation: 470
First Things first..
What I understand that you are new to C# programming and self taught. I would recommend Visual Studio for learning to program in C#, that will help you to figure out the issues quickly. The IDE will show red squiggly if there is any compile time errors or syntax errors, which I see in the above code you have pasted with the question. See below my
In the pic.. the Error show is ass Number is expecting 2 parameters separated by , and you are passing only 1 - you need to pass 2 numbers or 2 variables that are declared as double and has a value assigned.
In the above screen, you can see error, "result 1 does not exist in context - that mean you haven't declared result1 - you need to declare 2 variables of type double.
In the above screen you see another error - unexpected character $ - not sure where you got this and what were you trying to write with $ sign..
See in the above screen Console.WriteLine is expecting String and param object, which you provided correctly, then why did you add $ sign..
As I think you really need Visual Studio, so that you will be able ot fix many of these issue on your own..
Here is the complete running program.
namespace FunctionCalls
{
class Functions
{
static public double addNumbers(double number1, double number2)
{
double result1 = number1 + number2;
return result1;
}
static public double subtractNumbers(double number1, double number2)
{
double result2 = number1 - number2;
return result2;
}
static public double avgNumbers(double number1, double number2)
{
double result3 = (number1 + number2) / 2;
return result3;
}
static void Main(String[] args)
{
double result1 = 20.00;
double result2 = 10.00;
double sum, sub, avg;
sum = Functions.addNumbers(result1, result2);
sub = Functions.subtractNumbers(result1, result2);
avg = Functions.avgNumbers(result1, result2);
Console.WriteLine("The sum of your numbers is {0}", sum);
Console.WriteLine("The difference of your numbers is {0}", sub);
Console.WriteLine("The average of your numbers is {0}", avg);
Console.ReadKey();
}
}
}
and here is the Result screen shot..
Hope this Helps..
Happy Learning
please mark the it answered - if I was able to help in answering your question..
Upvotes: 0
Reputation: 24781
General Explanation
When you call a method, there are several components to the call.
var returnValue = StaticClassOrObject.MethodName(parameterOne, parameterTwo, etc);
// For Example
double sinValue = Math.Pow(2, 8);
MethodName
is the name of the method as it is defined. In the example, the name of the method is "Pow".
StaticClassOrObject
is the name of the static class or object reference that you are calling the method from. In the example, this is the static class "Math". (Note that if you are calling the method from inside the same class where the method is defined, this component is optional.)
parameterOne
, parameterTwo
, etc
are parameters that are passed to the method. If a method is declared as having a certain number of parameters, that many parameters must be passed when calling the method. In the example, the Math.Pow
method takes two parameters, so passing it any more or fewer will result in an error.
returnValue
is the value that the method returns upon its execution. If the method has a return type of void
, the method doesn't return anything. In the example, Math.Pow
returns a value of type double
, so I declare a variable of that type to hold the returned value. (By means of a shortcut, you can use the keyword var
to have the type of the variable be automatically set to the return type of the method.)
Your Error
The error in your program is two-fold. First, you only pass a single parameter in your method calls, but each of those methods are defined as requiring two parameters. Second, the parameters you are passing (result1
, result2
, result3
) aren't declared anywhere, so for the purposes of your program they don't exist.
Correcting these errors will give you something like the following:
var result1 = Functions.addNumbers(1, 2);
var result2 = Functions.addNumbers(3, 4);
var result3 = Functions.addNumbers(5, 6);
Though everything you are doing is within the Functions
class, so the ClassNameOrObject part of the call can be safely removed:
var result1 = addNumbers(1, 2);
var result2 = addNumbers(3, 4);
var result3 = addNumbers(5, 6);
Now these examples pass constant values as parameters. If you want to pass a variable as a parameter, you have to first declare and instantiate it:
double param1 = 1;
double param2 = 2;
var result1 = addNumbers(param1, param2);
Upvotes: 0
Reputation: 146597
The parentheses contain the method arguments (In your case, the numbers to add, subtract or average). If a method returns something, you have to assign it's value to a variable in the calling routine with an equal sign.
public static void Main(String[] args)
{
var result1 = Functions.addNumbers(13, 14);
var result2 = Functions.subNumbers(17, 18);
var result3 = Functions.avgNumbers(20, 21);
Upvotes: 3