Click to See Complete Forum and Search --> : passing a function to main in Java


chesskidd
10-07-2003, 07:38 AM
Hello guys, I'm learning Java at school this semester.
I have some background in C++ (intro level) before.

I'm writing a program and I want to pass a function and its values into main ( not sure if i called it correctly, what's the difference between a function and a method?, confused :confused: ). okay, i wrote this function inside the

public static void main(String args[])
{ ........
........ // ... means these are some codes
double a, b;
sorted (a, b);
.........
// then the function that i wrote:
public sorted(double a, double b)
{
double temp;
if( a < b)
{ temp=b;
b=a;
a=temp;
}
} // sort function
) // main method

afterall, i tried to compile the program, it gave me an error as below:
lab1.java:62: illegal start of expression
public sorted(double a, double b)
^

why??
something wrong with the declaration of the "function" ?
or the wrong function call in the main ? :(

any java guru? help is really appericated.

truls
10-07-2003, 08:57 AM
Move the function above the main function as in:
public sorted(double a, double b)
{
double temp;
if( a < b) {
temp=b;
b=a;
a=temp;
}
}

public static void main(String args[])
{
double a, b;
sorted (a, b);
) // main method
I'm a bit unsure if the sorted will work since it doesn't take references to the data members, but I could be getting Java and C++ mixed up.

je_fro
10-07-2003, 10:34 AM
I think you need to move sorted() outside of Main.

sclebo05
10-07-2003, 10:46 AM
i concur. both with the advice about moving the function out of main, and with the avatar of the post above me.

chesskidd
10-07-2003, 01:07 PM
hello everyone, first of all thank you for all the timely replies:)

i tried the way like u said, put the function outside the main,
but there is a funny thing that these swapped a, b variables won't return back to the main anymore... ( like how the reference or point in c).., is there a way for that?

i got a hint by my TA that i need to construct a "class" instead of using this. :rolleyes:

Modorf
10-07-2003, 01:43 PM
class container{
double a, b;
}

public container sorted(double a, double b)
{
container temp;
if( a < b) {
temp.a = b;
temp.b = a;
} else {
temp.a = a;
temp.b = b;
}
return temp;
}

public static void main(String args[])
{
container c;
double a, b;
c = sorted (a, b);
) // main method

chesskidd
10-07-2003, 02:45 PM
:eek: it's amazing...

Modorf, I really appreciate your help.

1 quick question that so this

class container{
double a, b;
}

public container sorted(double a, double b)
{
container temp;
if( a < b) {
temp.a = b;
temp.b = a;
} else {
temp.a = a;
temp.b = b;
}
return temp;
}

is in another class file?

i will try them.

oldaren
10-07-2003, 03:21 PM
You can have both classes in the same source file or in separate source files.

[If fact you can have as many classes as you like in one source file as long as only of the classes is public. If you have one public class in a source file, that file must have the same name as the public class.]