# Function of difference between two numbers

• May 13th, 2009, 03:47 AM
Function of difference between two numbers
Good Morning

The function takes two numbers as arguments and basically returns the difference between them. However I always need a positive number to be returned so the fucntion needs to take the smaller number from the larger one and this needs to be worked out within the function.

New to this so any help would be appreciated.

Thanks

JS
• May 13th, 2009, 04:29 AM
JavaPF
Re: Function
Hello uplink600 and welcome to the Java Programming Forums :D

I have written this code for you. It takes 2 integer values from the command line and returns the calculation.

Code :

```public class Uplink600 {   /** * JavaProgrammingForums.com */   public static int arg1, arg2;   public void Calculate(){   int result = 0;   // If statements to determine highest value if(arg1 <= arg2){ result = (arg2 - arg1); } if(arg1 >= arg2){ result = (arg1 - arg2); }   // Print our result to console System.out.println(result); }   public static void main(String[] args) {   // Error handling to check for arguments if(args.length < 2){ System.out.println("You must send 2 integer command line arguments!"); System.exit(0); }   // Parse String arguments to Integer arg1 = Integer.parseInt(args[0]); arg2 = Integer.parseInt(args[1]);   // Run Calculate Method Uplink600 uplink = new Uplink600(); uplink.Calculate();   }   }```
• May 13th, 2009, 05:57 AM
Freaky Chris
Re: Function
Here's my solution, based on the problem being. "Return the difference between the two characters, where no sign is present"
Code :

```public int Difference(int a, int b){ return Math.abs(a-b); }```
Regards,
Chris