Sign in to follow this  

[C# help] Functions.

This topic is 3313 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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 :)

Share this post


Link to post
Share on other sites
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);

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Quote:
Original post by DevFred
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.


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.

Share this post


Link to post
Share on other sites

This topic is 3313 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this