Java Reference
In-Depth Information
return
result ;
}
static double
F(
double
x)
double
result ;
r e s u l t =1.0+G( x
∗
x) ;
return
result ;
}
public static void
main ( String [ ]
args )
double
y;
y=F(5.0) ;
System . out . println (
"y="
+y ) ;
}
}
For example, considering the above program, the function call stack evolves as
follows:
Step
1
2
3
4
5
6
7
call
main
call
F
call
G
finish
G
finish
F
finish
main
Action:
run
G
F
main
Function
stack
F
main
F
main
∅
∅
main
main
Note that since arguments are passed by values, the local variables of the
calling
functions are not changed. To emphasize this point, consider the following toy
sample program:
Program 3.7
Pass-by-value does not change local variable of calling functions
class
PassByValue
{
static void
F(
double
x)
x=3;
// Here is the catch. Take care of pass-by-value.
System . out . println (
"Before exiting F, value of x:"
+x ) ;
}
public static void
main ( String [ ]
args )
double
x=5;
F(x) ;
System . out . println (
"value of x:"
+x ) ;
}
}
We get from the pass-by-value mechanism the following output:
Before exiting F, value of x:3.0
value of x:5.0
Search WWH ::
Custom Search