L09 - Computer Science 1 1 SS C Lecture 9 Contents" e...

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

View Full Document Right Arrow Icon
CS101 Lecture 9 1 Computer Science 1 1 SS C Lecture 9 Contents Methods Scope of variables Reasons for using methods Coursebook: §9 2 1 2 3 4 5 6 7 8 9 10 11 12 Review Methods and parameters – what is the output? public class MyProgram { } 0 "Hello" private void myMethod(int x, String s) { x = x + 1; System.out.println(s.charAt(x)); } public void start() { int x = 0; String welcome = "Hello"; myMethod(x, welcome); System.out.println(x); } e 0 3 1 2 3 4 5 6 7 8 Local Variables private void test() { int age = 10; } Illegal Variable 'age' is destroyed when the method ends Variables declared inside a method are local variables. Local variables are created when they are declared in a method, and are destroyed when method ends. public void start() { test(); System.out.println("age = " + age); } 4 1 2 3 4 5 6 7 8 9 10 11 Local Variables public void start() { System.out.println("Start method"); System.out.println("a = " + a); int a; a = 7; } Scope means "which lines of code can use the variable" public void start() { System.out.println("Start method"); int a; a = 7; System.out.println("a = " + a); } local variables may be used from the point they are declared until the end of the block in which they are declared Illegal 5 1 2 3 4 5 6 7 8 9 10 11 12 Same identifer, diFFerent methods Local variables can only be "seen" inside the method in which they are declared. Methods can use the same identiFer for local variables (there is no confusion). 0 public void start() { int age = 0; testing01(); testing02(); System.out.println(age); } private void testing02() { int age = 20; } private void testing01() { int age = 10; } 6 Method Execution When a method call is made, code execution jumps to the method. a = 23; b = doSomething(); c = 45; private int doSomething() { int c = 6; int d = c + 34; return d; } If the method returns a value. When the method reaches a return statement, code execution returns to the point of call and the method call is replaced by the value returned. 40
Background image of page 1

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

View Full DocumentRight Arrow Icon
CS101 Lecture 9 2 7 Method Execution a = 23; doSomething(); b = 45; private void doSomething() { int c = 380; int d = c + 34; int e = 84; } When a method call is made, code execution jumps to the method.
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 03/01/2012 for the course COMPSCI 101 taught by Professor Adrianaferraro during the Summer '12 term at Auckland.

Page1 / 4

L09 - Computer Science 1 1 SS C Lecture 9 Contents" e...

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

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