Você está na página 1de 24

Session 3

FUNCTIONS
Agenda

Solving Problems with functions


Function call trace
Recursion small intro
Libraries and Clients
Solving Problems

Example Problem
Lets solve together the Calculator Homework.
Solving Problems

Conclusion
We implemented several methods for solving little problems
How would code look like if we put everything in the main method?
We then used these methods in the main method
=> a method can call other methods in order to solve complex problems
Always try to break problems into sub-problems!
Functions are the first mechanism for doing this
Function mechanism is implemented in Java via static methods.

Rules of thumb
It is elegant to do one thing in one statement. If it doesnt fit, you can
implement a method for it.
You should implement one method whenever the same code is written
multiple times!

Flow of Control
Function call modifies the flow of control!
Java Methods

public double calculateAnswer(double wingSpan, int numberOfEngines,


double length, double grossTons) throws Exception1, Exception2 {
//do the calculation here
}

The only required elements of a method declaration are the method's return type, name, a
pair of parentheses, (), and a body between braces, {}.

More generally, method declarations have six components, in order:

Modifierssuch as public, private, and others you will learn about later.
The return typethe data type of the value returned by the method, or void if the method
does not return a value.
The method namethe rules for field names apply to method names as well, but the
convention is a little different.
The parameter list in parenthesisa comma-delimited list of input parameters, preceded by
their data types, enclosed by parentheses, (). If there are no parameters, you must use
empty parentheses.
An exception listto be discussed later.
The method body, enclosed between bracesthe method's code, including the declaration
of local variables, goes here.
Static Methods

By now we used pre-defined methods and wrote only the main() method
Java statements are grouped together in methods
Each method must be inside a class
static methods dont need an object (well see later what an object is):

Integer.parseInt(10);

Invoke method parseInt from class Integer.


No object (instance) of class Integer was created.
Function call trace

When calling a function:


1. We leave our current function and enter the new function
2. From there, we will eventually return back to our calling function in
the same point as we leaved.
int i = calculateValue(something);
Function call trace

When calling a function:


1. We leave our current function and enter the new function
2. From there, we will eventually return back to our calling function in
the same point as we leaved.
int i = calculateValue(something);
3. A function can have parameters: readInt(String input)
4. Functions have return type: int readInt(String input)
Function call trace

When calling a function:


1. We leave our current function and enter the new function
2. From there, we will eventually return back to our calling function in
the same point as we leaved.
int i = calculateValue(something);
3. A function can have parameters: readInt(String input)
4. Functions have return type: int readInt(String input)
5. Functions can have multiple return statements (exit points)
=> the flow of control can be non-linear inside the function
6. Functions can throw exceptions (well see later). In that case, it will not
return to the calling client.
Function call trace

When calling a function:


1. We leave our current function and enter the new function
2. From there, we will eventually return back to our calling function in
the same point as we leaved.
int i = calculateValue(something);
3. A function can have parameters: readInt(String input)
4. Functions have return type: int readInt(String input)
5. Functions can have multiple return statements (exit points)
=> the flow of control can be non-linear inside the function
6. Functions can throw exceptions (well see later). In that case, it will not
return to the calling client.

=> function can either return or throw an exception (marking an error


condition which needs to be treated somewhere)

Activity: visualize a call trace in class


Recursion

We saw how to invoke (call) one method from another method.


It is also possible to call a method from itself!
Recursion

We saw how to invoke (call) one method from another method.


It is also possible to call a method from itself!

public static int fibonacci (int n) {


if (n == 0 || n == 1) {
return n;
}
return fibonacci(n-1) + fibonacci(n-2);
}
Recursion

We saw how to invoke (call) one method from another method.


It is also possible to call a method from itself!

public static int fibonacci (int n) {


if (n == 0 || n == 1) {
return n;
}
return fibonacci(n-1) + fibonacci(n-2);
}

Please invoke this method from the main() method.


Recursion

We saw how to invoke (call) one method from another method.


It is also possible to call a method from itself!

public static int fibonacci (int n) {


System.out.println(n);
if (n == 0 || n == 1) {
return n;
}
return fibonacci(n-1) + fibonacci(n-2);
}

Please invoke this method from the main() method.


What happens when we add this println() in the recursive method? Discuss.
Clients and Libraries

Terminology
The method which calls another method is called a client.
The act itself is named function call or method invocation.
Clients and Libraries

Terminology
The method which calls another method is called a client.
The act itself is named function call or method invocation.
The main() method makes two function calls to the readInt() method
Clients and Libraries

Libraries
In our activity we deal with complex problems:
make secure online payments
process images (rotate, translate, resize, )
Implement a GUI (in Windows, Linux, Mac, ): we need windows,
buttons, check-boxes, etc
Clients and Libraries

Libraries
In our activity we deal with complex problems:
make secure online payments
process images (rotate, translate, resize, )
Implement a GUI (in Windows, Linux, Mac, ): we need windows,
buttons, check-boxes, etc
We dont want (and dont have time) to implement all these ourselves
Wed like to be able to include this functionality when needed and just call
some methods
=> concentrate on our problem and re-use code written by somebody else
Clients and Libraries

Libraries
In our activity we deal with complex problems:
make secure online payments
process images (rotate, translate, resize, )
Implement a GUI (in Windows, Linux, Mac, ): we need windows,
buttons, check-boxes, etc
We dont want (and dont have time) to implement all these ourselves
Wed like to be able to include this functionality when needed and just call
some methods
=> concentrate on our problem and re-use code written by somebody else
Welcome to libraries!
They simply contain classes and methods
And are packed in .jar files (similar to .zip files)
Clients and Libraries

Libraries
In our activity we deal with complex problems:
make secure online payments
process images (rotate, translate, resize, )
Implement a GUI (in Windows, Linux, Mac, ): we need windows,
buttons, check-boxes, etc
We dont want (and dont have time) to implement all these ourselves
Wed like to be able to include this functionality when needed and just call
some methods
=> concentrate on our problem and re-use code written by somebody else
Welcome to libraries!
They simply contain classes and methods
And are packed in .jar files (similar to .zip files)
e.g. opencv-2.4.6.jar means opencv (open computer vision) version 2.4.6
Clients and Libraries

Libraries
In our activity we deal with complex problems:
make secure online payments
process images (rotate, translate, resize, )
Implement a GUI (in Windows, Linux, Mac, ): we need windows,
buttons, check-boxes, etc
We dont want (and dont have time) to implement all these ourselves
Wed like to be able to include this functionality when needed and just call
some methods
=> concentrate on our problem and re-use code written by somebody else
Welcome to libraries!
They simply contain classes and methods
And are packed in .jar files (similar to .zip files)
e.g. opencv-2.4.6.jar means opencv (open computer vision) version 2.4.6
Using it is easy:
add the path of the .jar file to the CLASSPATH environment variable.
Import the needed package member (class, method, constant, ) in
your class
Activity

Implement a method which receives the file system


path to an image, loads the image, rotates it 90
degrees right and saves the new image in the same
folder as the original one, but with a slightly
different name.

Optional: the program can be interactive and accept


multiple commands: rotate left, etc.
Activity

Download scalar source code:


https://github.com/thebuzzmedia/im
gscalr
Compile: javac
org/imgscalr/Scalr.java
Create a jar file: jar cf scalar.jar
org/imgscalr/*.class
Implement rotate
Bibliography

Thinking in Java - 3: Controlling


Program Flow
https://docs.oracle.com/javase/tutori
al/java/javaOO/methods.html
https://docs.oracle.com/javase/tutori
al/java/javaOO/returnvalue.html

Você também pode gostar