5 Replies - 387 Views - Last Post: 03 May 2010 - 02:59 PM Rate Topic: -----

#1 WaseemAShaik  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 02-May 10

calling functions as parameters

Posted 02 May 2010 - 10:17 PM

Hi,

I have written 2 simple java classes and an application program.
I'm trying to call functions as parameters for another function, I'm getting this error
Exception in thread "main" java.lang.RuntimeException: Uncompilable source code
at prog8.Main.main(Main.java:21)
Java Result: 1
BUILD SUCCESSFUL (total time: 2 seconds)
package prog8;

/**
*
* @author Waseem Akram Shaik
*/
public class p8 {
private int A,B,C;

public p8()
{
int A=1, B=2, C=3;
}
public int fA()
{
System.out.println("This if function A");
return A;
}
public int fB()
{
System.out.println("This if function B");
return B;
}
public int fC()
{
System.out.println("This if function C");
return C;
}
}

package prog8;

/**
*
* @author Waseem Akram Shaik
*/
public class pg8 {
public void fun(f1(),f2(),f3())
{
int x=f1();
int y=f2();
int z=f3();
System.out.println("Order : "+x+y+z);
}
}

package prog8;

/**
*
* @author Waseem Akram Shaik
*/
public class Main {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
p8 p2 = new p8();
pg8 p1 = new pg8();
p1.fun(p2.fA(),p2.fB(),p2.fC());
//p.fA();
}
}

your help would be appreciated...THANKS

This post has been edited by WaseemAShaik: 02 May 2010 - 10:25 PM


Is This A Good Question/Topic? 0
  • +

Replies To: calling functions as parameters

#2 pdkharkar  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 63
  • View blog
  • Posts: 345
  • Joined: 19-January 09

Re: calling functions as parameters

Posted 02 May 2010 - 11:37 PM

when you are writing the code for the method fun() you need to write the types of the parameters also but it is not required when you are passing arguments
also do the following things
in the class p8 declare and define the instance variables outside constructor
private int A=1, B=2, C=3;

your function fa(), fB() and fC() are returning the int values isn't it?
in the pg8 class change the function to
public void fun(int x1,int y1,int z1)
{
int x=x1;
int y=y1;
int z=z1;
System.out.println("Order : "+x+y+z);
}

thus the methods you are passing will return the respective integers
these integers will be assigned to the int x , int y and int z in the fun method and the System.out.println() method will be executed
keep the main class intact
there is no need to change it
hope this solves the problem because I have got the outout :)
what exactly happens here is that
when you write
p1.fun(p2.fA(),p2.fB(),p2.fC());

method fun() is called by object referenced by p1 and in the arguments of the method
that requires integer arguments, the methods fA(), fB() and method fC() return those respective values
these values are then passed INTO the function and are used there that are stored in those x, y and z variables where we have printed them
Hope this helps :)
Happy coding :)

This post has been edited by pdkharkar: 02 May 2010 - 11:40 PM

Was This Post Helpful? 0
  • +
  • -

#3 Galois  Icon User is offline

  • D.I.C Head

Reputation: 28
  • View blog
  • Posts: 207
  • Joined: 27-March 10

Re: calling functions as parameters

Posted 03 May 2010 - 12:00 AM

Functions as parameters? My friend, functional programming is that-a-way ====>
Was This Post Helpful? -1
  • +
  • -

#4 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1011
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: calling functions as parameters

Posted 03 May 2010 - 02:51 AM

@WaseemAShaik
next time, please post your code inside code tags.
it makes your code much easier to read :)
Was This Post Helpful? 0
  • +
  • -

#5 WaseemAShaik  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 02-May 10

Re: calling functions as parameters

Posted 03 May 2010 - 07:37 AM

Thank you guys.....got it...
Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10767
  • View blog
  • Posts: 40,093
  • Joined: 27-December 08

Re: calling functions as parameters

Posted 03 May 2010 - 02:59 PM

View PostGalois, on 03 May 2010 - 03:00 AM, said:

Functions as parameters? My friend, functional programming is that-a-way ====>

That may be true, but it is completely legal to feed invocations for methods with non-void return types as the params for other methods, so long as the return type matches the param type.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1