06-recursion - Methods and Recursion CSE 114 Computer...

Info iconThis preview shows pages 1–10. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Methods and Recursion CSE 114: Computer Science I 1 Software Reuse • Laziness is a virtue among programmers • Often, a given task must be performed multiple times • Ex. loops for local repetition • Instead of (re)writing the code each time, it is more efFcient to write the code once and reuse it as necessary 2 Methods • A function is a small block of code that can be called from another point in a program • In Java, functions are called methods • Functions/methods enable reuse, and can be used to abstract out common tasks • Ex. computing the tax on a purchase • Method results can be changed by supplying different input values 3 Method Examples • main() • System.out.print() • Integer.parseInt() • String.length() • String.toUpperCase() 4 Method Arguments • Arguments are pieces of data that are passed into a method • Different input can produce different results • Arguments can be manipulated, like variables • Primitive type arguments are passed as copies — changes are not sent back. • Objects may change when passed as arguments 5 Method Execution • Only one method can be active at a time • When a method is invoked, the calling method is put on hold while the new method executes. • When the called method completes (returns), execution returns to the calling method 6 Method Execution • A method may call other methods. Method calls can be nested. • Suppose A calls B, which calls C, etc. 1. A is put on hold until B fnishes 2. B is put on hold until C fnishes 3. AFter C, B resumes, then A resumes 7 Defning a Method • A method defnition consists oF a method header and a method body • The method header specifes the return type, name, and arguments list • Ex. public static void main(String args) • The method body is a brace-enclosed set oF 0 or more program statements 8 Method Example int multiply (int frst, int second) // header { return (frst * second); // body } 9 Another Example...
View Full Document

Page1 / 41

06-recursion - Methods and Recursion CSE 114 Computer...

This preview shows document pages 1 - 10. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online