Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 [Tutorial] Parameters & Return Values
Index -> Programming, Java -> Java Tutorials
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Tat




PostPosted: 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.
Ex: play(100);
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.
Java:

        Ex:     boolean smart(int test1, int test2, int test3)
                {
                        if(test1>90 && test2>90 && test3>90)
                                return true;
                        else
                                return false;
                }


5 short answer questions
1) What's the error with the following code?
Java:

int score()
{
        String name=reader.readLine();
        return name;
}

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?
Arguments.
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:
Actual Parameters
Formal Parameters
Return values
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...



Parameters & Return Values.doc
 Description:
if u want it to view it alot better dl and view it with microsoft words

Download
 Filename:  Parameters & Return Values.doc
 Filesize:  21 KB
 Downloaded:  405 Time(s)

Sponsor
Sponsor
Sponsor
sponsor
Display posts from previous:   
   Index -> Programming, Java -> Java Tutorials
View previous topic Tell A FriendPrintable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 1 Posts ]
Jump to:   


Style:  
Search: