CSE 14 Lecture 8: Functions
CMPS12A Lecture 8 – Functions
When calling functions, you must remember that Java goes into the code of main() first and it is
only when there is a function in main where that function outside of main will be called.
Example:
static String getName() {
Scanner sc = new Scanner(System.in);
“ste.out.pitEte ou ae: ;
return sc.next();
static void printMessage(String s) {
“ste.out.pitHello, ;
System.out.println(s);
}
public static void main(String [] args) {
String name = getName();
printMessage(name);
}
In this example, the data type is String, declared in the function --> static String getName()
hih eas that a “tig tpe has to e etued. Reee, sie oid is’t used, e ust
return something, but if void was use, then we would just continue with statements. In the
function -> static void printMessage(String s), you can see here that nothing is returned but
rather whatever statements you want to change without returning anything or print something
(and it will make sense you do more coding), you would add void to the function.
Now, as you can see in main(), we initiate a String of variable name to be the function
getNae. Thus, he e go to that futio, e etu use iput etu s.et , thus
whatever that is, name is assigned that input. Now you see function printMessage(name),
hih e o go ito that futio ad see that Hello , is pited out ad the pits out s,
find more resources at oneclass.com
find more resources at oneclass.com
hih is the futio’s paaete, “tig s. I ai, e ae allig that fution with name as
its parameter, thus we must replace that placeholder of String s, (means it has to be a String
type), with the value of what we are trying to use. Look at it like this, the function we make is
basically a blueprint where we construct where you want to put things with the type that you
want to put. Then, when we go to main(), we must use that function with a variable of that
same type that was used to make that function. Sorry if it sounds confusing but you will get the
hang of it!
Example:
static type fcn(Param-list) {
//local variables
//executable stunts
//something here
return exp;
Thus as you see here, the function must return the same type initiated in the beginning
when you constructed the function, -> static type fcn(Param-list). That type can be a
String, int, double, an array of integers like int[], etc. The Param-list is basically the
placeholder of the function, as I like to call it, that whatever code you put into this
function, of course using the Param-list in your code, that placeholder will be replaced
with the actual variable you are trying to use for the function. The Param-list can be
anything like int variable, String variable, String [] variable, etc. Thus, exp here should be
the sae tpe fo the futio’s tpe to be returned.
Example:
static void fcn(Param-list) {
//something here
//something here
//something with parameter
}
See how nothing is returned. void makes the function execute and whatever code you
put in here, a stand-alone statement that you can use easily by just calling on the
function, of course with the right variables.
find more resources at oneclass.com
find more resources at oneclass.com
Document Summary
When calling functions, you must remember that java goes into the code of main() first and it is only when there is a function in main where that function outside of main will be called. (cid:455)ste(cid:373). out. p(cid:396)i(cid:374)t(cid:894)(cid:862)e(cid:374)te(cid:396) (cid:455)ou(cid:396) (cid:374)a(cid:373)e: (cid:862)(cid:895); return sc. next(); static void printmessage(string s) { System. out. println(s); public static void main(string [] args) { In this example, the data type is string, declared in the function --> static string getname() (cid:449)hi(cid:272)h (cid:373)ea(cid:374)s that a t(cid:396)i(cid:374)g t(cid:455)pe has to (cid:271)e (cid:396)etu(cid:396)(cid:374)ed. Re(cid:373)e(cid:373)(cid:271)e(cid:396), si(cid:374)(cid:272)e (cid:448)oid is(cid:374)"t used, (cid:449)e (cid:373)ust return something, but if void was use, then we would just continue with statements. Now, as you can see in main(), we initiate a string of variable name to be the function getna(cid:373)e(cid:894)(cid:895). Thus, (cid:449)he(cid:374) (cid:449)e go to that fu(cid:374)(cid:272)tio(cid:374), (cid:449)e (cid:396)etu(cid:396)(cid:374) use(cid:396) i(cid:374)put (cid:271)(cid:455) (cid:862)(cid:396)etu(cid:396)(cid:374) s(cid:272). (cid:374)e(cid:454)t(cid:894)(cid:895)(cid:863) , thus whatever that is, name is assigned that input.