{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

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

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

View Full Document Right Arrow Icon
Methods and Recursion CSE 114: Computer Science I 1
Image of page 1

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

View Full Document Right Arrow Icon
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 efficient to write the code once and reuse it as necessary 2
Image of page 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
Image of page 3

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

View Full Document Right Arrow Icon
Method Examples main() System.out.print() Integer.parseInt() String.length() String.toUpperCase() 4
Image of page 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
Image of page 5

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

View Full Document Right Arrow Icon
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
Image of page 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 finishes 2. B is put on hold until C finishes 3. After C, B resumes, then A resumes 7
Image of page 7

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

View Full Document Right Arrow Icon
Defining a Method A method definition consists of a method header and a method body The method header specifies 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
Image of page 8
Method Example int multiply (int first, int second) // header { return (first * second); // body } 9
Image of page 9

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

View Full Document Right Arrow Icon
Image of page 10
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern