0

doing some uni work on 'Processing' Programming language (a form of java).

So my question is 'Write a function called twoNumbers(int a,int b) which takes in two parameters a and b. If a is greater than b, then the two numbers are added together and the string 'the sum of a and b is sum' is displayed in the console window, where a and b and the sum are the values of a, b and their sum. Finally, the function should return the sum.'

..soo here is my attempt at the code, if I put (int a,int b) after the customer function, it just says that my other int a = number, is a duplicate, which is true, but im not sure how I am ment to give a and b a number without it thinking its a duplicate? Should I be putting it out of a void setup tag? as im unsure if this would then cause too many { brackets...

/* Question 1 */

int twoNumbers(){
int a = 30;
int b = 20;
 if (a > b) {println(a+b);}
  println("The sum of a and b is sum");
  int sum;
  sum = a+b;
  println(sum);
}

Any help would be massively helpful in getting this and the other questions done :)

Thanks!!

1
  • Use the method signature like in your assignment: public static int twoNumbers(int a,int b) and don't create new variables with the same names. Just use those variables. Also show us your main method, where you call this method. Commented Nov 7, 2012 at 14:45

2 Answers 2

2

Also your function is not returning a value, which will give you an error. It looks like you are confusing things. Either declare it a void or return a value of declared type (that last is what your assignment calls for). Either way a function, or a method, needs to be called to execute, and you are not calling it! So the code inside your the function is not being run!! The following:

void imAMethod()
{
  println("hello");
}

It is a valid method, but will do nothing, you need to call it, like:

imAMethod();// calling your method

void imAMethod()
{
  println("hello");
}

But this won't work also, will give you the error "It looks like you're mixing "active" and "static" modes". Thats because to use a function in Processing you need to have at least a setup() method in the sketch, so:

 void setup()
{
  imAMethod();
}//end of setup

void imAMethod()
{
println("hello");
}

will work as expected.

But you need a function, so as Jesper pointed you will have to do something like:

int a = 30; // those are global variables to pass to your function
int b = 20;
void setup()// this is a builtin basic Processing method
 {
   //call your function 
   println("The sum of " + a + " and " + b + " is "+ twoNumbers(a, b));
 }

int twoNumbers(int a, int b)
{ 
//do your math and tests here
return result;
}

There is another thing not clear in the assignment. A function must return something, so it is not clear what the function should return if a is not greater than b. You will have to handle this case, or compiler will complain. You may want to move this test out of the function to make things easier, like:

if (a < b)
println("The sum of " + a + " and " + b + " is "+ twoNumbers(a, b));//call your function
else
println(a + " is smaller than " + b);

and in the function just do the sum. But this may be not what the assignment requires... Anyway you will need to return something even if a is not greater than b. Note that the printing to console can be done inside the function also.

Hummm, re reading the assignment a think what is expected is: Aways return the sum, and just print if a is greater than b, which makes more sense and is easier, something like:

int twoNUmbers(int a, int b)
{
 if (a < b){/*print the string*/}
 return a + b;
}

Just a note for jlordo. In Processing.org you don't have a main, or better, it is transparent/hidden from user. Processing is like a "dialect" of java. So the code above would run as it is. There are two basic builtin functions: setup() and draw(). If the user do not use none of them the IDE will warps it in a setup() function, that will call the main() somewhere else. It will run once. Draw() instead loops forever.

Sign up to request clarification or add additional context in comments.

Comments

1

'Write a function called twoNumbers(int a,int b) which takes in two parameters a and b.

That's not what your code looks like. Your method twoNumbers doesn't take two parameters a and b. Your code should start like this (exactly as mentioned in the assignment):

int twoNumbers(int a, int b) {

Remove the next two lines, int a = 30; and int b = 20;. Those lines declare two local variables named a and b. You should use the a and b that are passed in as parameters instead.

This also looks wrong:

if (a > b) {println(a+b);}
println("The sum of a and b is sum");

Carefully look at what the assignment says:

If a is greater than b, then the two numbers are added together and the string 'the sum of a and b is sum' is displayed in the console window, where a and b and the sum are the values of a, b and their sum.

That's not what your code is doing. Take it step by step, carefully think about what is meant in the assignment.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.