Presentation is loading. Please wait.

Presentation is loading. Please wait.

Methods/Functions.

Similar presentations


Presentation on theme: "Methods/Functions."— Presentation transcript:

1 Methods/Functions

2 What is a method? A named section of code
Code reuse: Write once, run it many times In Java all methods are part of classes Static methods can be called without declaring an object of the class first.

3

4 Function calling Prompt this code to run by calling the function.

5 Detailed description about how to use a word
I want to sing the chorus Their 'songs' are structured by contrasting textures and energies rather than verses and choruses Use word in separate text Refer to a word without its whole definition Detailed description about how to use a word Function Call Function Definition Use function in separate program. Refer to it by name Detailed code. How to use the function if it returns value public static void main(String[] args) { // TODO Auto-generated method stub chorus(); } private static void chorus() { // TODO Auto-generated method stub }

6 Parameters and Arguments
Parameters: Variables in the parentheses of our function, that we can then use inside our function. Arguments: Specific values passed into our function call (ex. “Tony”) When you call a method, the arguments used must match the declaration's parameters in type and order.

7 Argument Order printPhoto (10,20,false); printPhoto (20,10,false);
Matching the argument with the type of the parameters. Keep in mind that the order and type are very important. When you use multiple parameters and put arguments in a function call.

8 Return values Function Input output Parameters Return values
Defined by return type Not only functions can take the input as parameters, they can also return data to you. This data is called return values of this function. Void means not returning any data Printing does not create output that we can interact with later on in separate programs. Done in internal code. public void functionName() { //internal block of code } In order to return values, changing the void to int, String, Boolean etc.

9 Demo Calculate the area and perimeter of a rectangle.

10 Return values To return a value you need:
A return type - void, int, String, etc A return statement -- line of code that returns a value Return values let you use values in other function calls or programs !

11 Demo Methods working with Array


Download ppt "Methods/Functions."

Similar presentations


Ads by Google