A Foundation for Programming

2.1 Functions A Foundation for Programming any program you might want to write objects functions and modules build bigger programs and reuse cod...
Author: Amberly Oliver
7 downloads 3 Views 936KB Size
2.1 Functions

A Foundation for Programming

any program you might want to write

objects

functions and modules

build bigger programs and reuse code

graphics, sound, and image I/O arrays conditionals and loops Math

primitive data types

text I/O

assignment statements

2

Functions • Take in input arguments (zero or more) • Perform some computation - May have side-effects (such as drawing)

• Return one output value Input Arguments

x y z

Return Value

f

f (x, y, z)

Functions (Static Methods) • Applications: - Use mathematical functions to calculate formulas - Use functions to build modular programs • Examples: - Built-in functions:

,

,

Math.random() Math.abs() Integer.parseInt()

- I/O libraries:

,

,

ellipse() beginShape() StdAudio.play()

- User-defined functions: main() 4

Modularity • Breaking programs into smaller pieces

• Right now your setup/draw probably looks like a lot of code • Breaking it up into logical pieces • drawSnail() was a function • If it as t a fu tio …..

5

Anatomy of a Java Function • Java functions – It is easy to write your own - Example: double sqrt(double c) 2.0

input

sqrt(c) = c

return type

output

method name

1.414213…

arguments

public static double sqrt(double c) { ... method signature } (excludes return type) Please note that the method’s signature is defined incorrectly in the figure on pg 188 of your textbook 6

Anatomy of a Java Function Example - absolute value • Step 1 – think of what the function is doing. Finding absolute value • Step 2 – give it a name. Name of a function = important!! abs • Step 3 – hat goes i to the fu tio . What are the argu e ts . What type are the arguments? single integer. abs (int x)

• Step 4 - what does it return? what datatype comes out of the function. Another integer. int abs (int x) • Step 5 - put public static before it and create a method body

public static int abs(int x){ // fill your code here }

7

Flow of Control Functions provide a new way to control the flow of execution The sqrt function is written here using the Newton method. Do not worry about the actual code inside sqrt

implicit return statement at end of void function

8

Flow of Control What happens when a function is called: - Control transfers to the function - Argument variables are assigned the values given in the call - Function code is executed 1 - Return value is substituted in place of the function call in the calling code - Control transfers back to the calling code

Note: This is known as "pass by value"

3

2

9

Organizing Your Program • Functions help you organize your program by breaking it down into a series of steps - Each function represents some abstract step or calculation - Arguments let you make the function have different behaviors • Write something ONCE as a function then reuse it many times - That is also alled the D‘Y (Do t ‘epeat Yourself) principle - As opposed to WET (Write Everything Twice / We Enjoy Typing) 10

Scope Scope: the code that can refer to a particular variable - A variable's scope is the entire code block (any any nested blocks) after its declaration

Simple example: int count = 1; for (int i = 0; i < 10; i++){ count *= 2; } // using 'i' here generates // a compiler error

Best practice: declare variables to limit their scope

11

Scope with Functions

12

Tracing Functions public class Cubes1 { public static int cube(int i) { int j = i * i * i; return j; } public static void main(String[] args) { int N = Integer.parseInt(args[0]); for (int i = 1; i January 1, 2000 Pre-condition: 1