CSE 14 Lecture 9: More Functions

40 views6 pages
1 May 2018
School
Course
Professor
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
Unlock document

This preview shows pages 1-2 of the document.
Unlock all 6 pages and 3 million more documents.

Already have an account? Log in
ad etu . Notie that thee as o eed to ake a if  <  stateet sie
else alead 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 futio, e use itege  ad itege d’s atual alues to eplae  ad  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.pitla +  ad  + ;
swap(a, b);
“ste.out.pitla + ad  +  +  afte sap;
static void swap(int x, int y) {
int temp = x;
x = y;
y = temp;
}
To udestad hat is happeig hee, let’s t to tae the ode ad see h this
sap. Also, otie ho sap i ai does’t eual athig. “ie it is a oid function,
it asiall just hages thigs i the ode thus it does’t hae to etu athig.
find more resources at oneclass.com
find more resources at oneclass.com
Unlock document

This preview shows pages 1-2 of the document.
Unlock all 6 pages and 3 million more documents.

Already have an account? Log in

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.

Get access

Grade+
$40 USD/m
Billed monthly
Grade+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
10 Verified Answers
Class+
$30 USD/m
Billed monthly
Class+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
7 Verified Answers

Related Documents

Related Questions