| Posted: Thu Feb 05, 2004 8:44 pm Post subject: [Tutorial] Parameters & Return Values
|Parameters & Return Values
The string of characters that appears between the parentheses following the message is called a parameter. There are two types of parameter, formal and actual.
Formal parameters are listed in the method's definition.
Ex: void play(int num)
The bolded codes are the formal parameters. You must declare each variable in the parameter where you start defining a method.
Values passed to a method when it is invoked are called arguments or actual parameters.
This line of code is usually called in the main method or other methods. 100 is the actual parameter and now represents num in the other example.
Methods with main data types (int, char, String, double, boolean) as signature must have return values, other types of signatures cannot have return values like "void". Only codes above the return statement will be executed, all other codes below will be skipped. A method cannot have more than 2 return values and all return values must match the method's signature.
Ex: boolean smart(int test1, int test2, int test3)
if(test1>90 && test2>90 && test3>90)
5 short answer questions
1) What's the error with the following code?
name is a String which does not match the signature
2) What are the two types of parameter?
The two types of parameter are formal parameters and actual parameters.
3) Where are formal parameters found?
They can be found in the method's definition.
4) What is another name for actual parameters?
5) Can a method have more than 2 return values?
No, a method can only have 1 and only 1 return value.
Sample Problem(small coding assignment)
Code a program that asks for your name, place you live in and age using all of the followings:
After you've asked for their name, place you live in and age, display them like the followings:
John Smith(name) is 15(age, if age is 1 or less put year instead of years) years old living in Manhattan(place)
Note: do not display the things I wrote in parentheses, they are for personal references only.
MOD: +bits for another tutorial sumbited, could uses some code tags tho...
|if u want it to view it alot better dl and view it with microsoft words
|| Parameters & Return Values.doc
|| 21 KB
|| 256 Time(s)