5 Replies - 533 Views - Last Post: 23 October 2010 - 11:24 PM Rate Topic: -----

#1 Guest_Jennifer*


Reputation:

Writing Methods

Posted 21 October 2010 - 08:00 PM

How would I go about writing a method that accepts a number as a argument and returns a number using a formula?
Is This A Good Question/Topic? 0

Replies To: Writing Methods

#2 Moshambi  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 8
  • View blog
  • Posts: 280
  • Joined: 20-November 07

Re: Writing Methods

Posted 21 October 2010 - 09:02 PM

That is pretty generic in description but:

public void calculate(int x)
{
	return (x * x);  //this would return the function you want
}


This post has been edited by Moshambi: 21 October 2010 - 09:03 PM

Was This Post Helpful? -1
  • +
  • -

#3 TheRaiderNation  Icon User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 81
  • Joined: 11-January 10

Re: Writing Methods

Posted 21 October 2010 - 09:03 PM

So do you mean take in some kind of int, double, etc. parameter and return an int, double, etc. after it has gone through an equation?

EDIT: nvm, Moshambi beat me to it

This post has been edited by TheRaiderNation: 21 October 2010 - 09:04 PM

Was This Post Helpful? 0
  • +
  • -

#4 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2194
  • View blog
  • Posts: 5,222
  • Joined: 10-September 10

Re: Writing Methods

Posted 22 October 2010 - 01:43 PM

Except a method declared 'void' can't return anything. You'll get an error. If you want to return an 'int', you'll declare the method int rather than void, like:

public int myIntMethod (int x, int y)
{
}


This post has been edited by GregBrannon: 22 October 2010 - 01:44 PM

Was This Post Helpful? 2
  • +
  • -

#5 NeoTifa  Icon User is offline

  • Whorediot
  • member icon





Reputation: 2498
  • View blog
  • Posts: 15,469
  • Joined: 24-September 08

Re: Writing Methods

Posted 22 October 2010 - 03:10 PM

:D
http://www.dreaminco...or-beginners-3/

</shameless self promoting>
Was This Post Helpful? 1
  • +
  • -

#6 Moshambi  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 8
  • View blog
  • Posts: 280
  • Joined: 20-November 07

Re: Writing Methods

Posted 23 October 2010 - 11:24 PM

View PostGregBrannon, on 22 October 2010 - 01:43 PM, said:

Except a method declared 'void' can't return anything. You'll get an error. If you want to return an 'int', you'll declare the method int rather than void, like:

public int myIntMethod (int x, int y)
{
}




I knew something looked a little off when I wrote that out, thanks for correcting it.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1