Skip to Main Content

return function

User_4NU9SOct 12 2013 — edited Oct 23 2013

Plz help me in understanding this ...

case1)

class Y3

{

static int test1()

{

System.out.println("from test1");

return 100;

}

static int test2()

{

System.out.println("from test2");

return test1();

}

public static void main(String[] args)

{

//test1();

//System.out.println(test2());

//System.out.println(test2()+test1());

test2();

}

}

output: from test2

            from test1

So , when test2 is called in main method , the control went to test2() and started executing it , so when the ctl came to return of test2() , it saw test1() and ctl when to test1(), so when ctrl came to return of test1() ,, what happens then ??

case 2)

class Y3

{

static int test1()

{

System.out.println("from test1");

return 100;

}

static int test2()

{

System.out.println("from test2");

return 100;

}

public static void main(String[] args)

{

//test1();

//System.out.println(test2());

//System.out.println(test2()+test1());

test2();

}

}

output: from test2

In this case when ctl when to test2() and then to it return why its not printing its 100 . .. where as in case 1) , it executed its return thats why it when to test1() ..

Can any one explain me what is the correct way to understand this.

thanks

Abhishek

This post has been answered by unknown-7404 on Oct 12 2013
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked due to inactivity on Nov 20 2013
Added on Oct 12 2013
6 comments
2,660 views