CSE 14 Lecture 8: Functions

41 views5 pages
27 Apr 2018
School
Course
Professor
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.pitEte ou ae: ;
return sc.next();
static void printMessage(String s) {
“ste.out.pitHello, ;
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()
hih eas that a “tig tpe has to e etued. Reee, sie 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
getNae. Thus, he e go to that futio, e etu use iput  etu s.et , thus
whatever that is, name is assigned that input. Now you see function printMessage(name),
hih e o go ito that futio ad see that Hello , is pited out ad the pits out s,
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 5 pages and 3 million more documents.

Already have an account? Log in
hih is the futio’s paaete, “tig s. I ai, e ae allig that fution 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 sae tpe fo the futio’s tpe 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
Unlock document

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

Already have an account? Log in

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.

Get access

Grade+20% off
$8 USD/m$10 USD/m
Billed $96 USD annually
Grade+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
40 Verified Answers
Class+
$8 USD/m
Billed $96 USD annually
Class+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
30 Verified Answers

Related Documents

Related Questions