CSE 14 Lecture 9: More Functions
CMPS12A Lecture 9 – More Functions
As stated before, there are two basic ways of writing out a function, either a void one or
a non-void one. To call a non-void function:
var = fcn(arg-list);
//for arg-list exp1, exp2, …
One example:
Example:
class Max{
public static void main(String [] args) {
int c= 10, d = 5;
int m;
m = max(c, d);
System.out.println(m)l
}
static int max(int x, int y) {
if (x > y)
return x;
else
return y;
}
}
In this example, we go into main first like always and see that int m equals the function
max with param-list int c and int d. We then go into that function and see that there are
two return statements. At least one has to be reachable in order for the compiler to
compile the java file. Anyways, here it is basically saying that if x is greater than y, it
would return x. If the that is not true, it would automatically go into the else statement
find more resources at oneclass.com
find more resources at oneclass.com
ad etu . Notie that thee as o eed to ake a if < stateet sie
else alead does that. No, as stated from earlier lectures, the int x and int y are just
placeholders that only state what type the param-list variables need to be as well as the
return function of integer type.
static int max(int x, int y) //Here is says it must return an integer type, using
//integer x and integer y as its parameters.
I the ai futio, e use itege ad itege d’s atual alues to eplae ad i
the max function and proceed with the function using them. Thus, the output here
would be 10.
Now, how can one be able to swap values in order? We can make a swap function that
does that.
Example:
public static void main(String [] args) {
int a = 6, b = 7;
“ste.out.pitla + ad + ;
swap(a, b);
“ste.out.pitla + ad + + afte sap;
static void swap(int x, int y) {
int temp = x;
x = y;
y = temp;
}
To udestad hat is happeig hee, let’s t to tae the ode ad see h this
sap. Also, otie ho sap i ai does’t eual athig. “ie it is a oid function,
it asiall just hages thigs i the ode thus it does’t hae to etu athig.
find more resources at oneclass.com
find more resources at oneclass.com
Document Summary
As stated before, there are two basic ways of writing out a function, either a void one or a non-void one. To call a non-void function: var = fcn(arg-list); Example: class max{ public static void main(string [] args) { int c= 10, d = 5; int m; m = max(c, d); System. out. println(m)l static int max(int x, int y) { if (x > y) return x; else return y; At least one has to be reachable in order for the compiler to compile the java file. Anyways, here it is basically saying that if x is greater than y, it would return x. If the that is not true, it would automatically go into the else statement a(cid:374)d (cid:396)etu(cid:396)(cid:374) (cid:455). Noti(cid:272)e that the(cid:396)e (cid:449)as (cid:374)o (cid:374)eed to (cid:373)ake a(cid:374) (cid:862)if (cid:894)(cid:454) < (cid:455)(cid:895)(cid:863) state(cid:373)e(cid:374)t si(cid:374)(cid:272)e (cid:862)else(cid:863) al(cid:396)ead(cid:455) does that. //here is says it must return an integer type, using. //integer x and integer y as its parameters.