[C# help] Functions.
I want to write a function that will return an int value and will get the int vars - a and b.
I tried to do it like that - public int func(int a, b) {}, but it didn't work.
How can I write this function right?
The function itself works, thanks, but I've got another problem now. This is my code:
namespace ConsoleApplication17
{
class Program
{
public double func(double a, double b)
{
double c = 0;
for (int i = 1; i <= b - 1; i++)
{
if (c == 0)
{
c = a * a;
}
else if (c != 0)
{
c = c * a;
}
}
return c;
}
static void Main(string[] args)
{
double d = Program.func(2, 3);
}
}
}
I tried to call the function from Main, but it doesn't work. What do I need to do?
Another thing - I've been looking for a guide for function in C# but haven't found one. Anyone has one?
namespace ConsoleApplication17
{
class Program
{
public double func(double a, double b)
{
double c = 0;
for (int i = 1; i <= b - 1; i++)
{
if (c == 0)
{
c = a * a;
}
else if (c != 0)
{
c = c * a;
}
}
return c;
}
static void Main(string[] args)
{
double d = Program.func(2, 3);
}
}
}
I tried to call the function from Main, but it doesn't work. What do I need to do?
Another thing - I've been looking for a guide for function in C# but haven't found one. Anyone has one?
Quote:Original post by Portishead
Another thing - I've been looking for a guide for function in C# but haven't found one. Anyone has one?
Functions in C# operate much the same way as functions in C++ and I assume many other languages, but Google is always your friend. What in particular did you want material on? "Functions" is a large category of information :)
you defined the function as a normal member funciton (non-static), but are calling it like a class function (static). you can google these concepts because you need to learn them ... but just understand that functions written in languages like C++ or C# or Java (Object-Oriented languages) that are inside of a class, are called on INSTANCES of that class ... like:
Program p = new Program();
double result = p.func(5.0, 7.0);
Program p = new Program();
double result = p.func(5.0, 7.0);
You are going to have to research on what static members/classes are. Main is a static method, where the class it resides in does not have to be created. func(...) is not static, and has to be called as a member of an obejct (which doesn't exist), so you cannot call it. The easiest would be making func static.
Okay, it all works now.
I tried this in Main() - "Console.WriteLine(func(2,3));". It worked.
When I tried this in Main() - "double d = func(2,3);" - it returned an error.
The function returns a var at the end.
What am I doing wrong?
Thanks everyone for all the help.
I tried this in Main() - "Console.WriteLine(func(2,3));". It worked.
When I tried this in Main() - "double d = func(2,3);" - it returned an error.
The function returns a var at the end.
What am I doing wrong?
Thanks everyone for all the help.
Quote:Original post by Xai
just understand that functions [...] that are inside of a class, are called on INSTANCES of that class
No, they are called member functions or methods. An instance is the result of a constructor call.
Quote:Original post by DevFredQuote:Original post by Xai
just understand that functions [...] that are inside of a class, are called on INSTANCES of that class
No, they are called member functions or methods. An instance is the result of a constructor call.
Bold emphasis mine. Xai, I assume, is referring to the the need for creating an instance of a class before being able to use its member functions.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement