comp15111: introduction to architecturesyllabus.cs.manchester.ac.uk/.../lectures/stack2.pdf ·...

22
COMP15111: Introduction to Architecture Lecture 10: Methods – Parameters and Variables Dr. Javier Navaridas School of Computer Science, University of Manchester Autumn 2016 COMP15111 Lecture 10 1/33

Upload: others

Post on 09-May-2020

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: COMP15111: Introduction to Architecturesyllabus.cs.manchester.ac.uk/.../lectures/stack2.pdf · Lecture 10: Methods – Parameters and Variables Dr. Javier Navaridas School of Computer

COMP15111: Introduction to ArchitectureLecture 10: Methods – Parameters and Variables

Dr. Javier Navaridas

School of Computer Science, University of Manchester

Autumn 2016

COMP15111 Lecture 10 1/33

Page 2: COMP15111: Introduction to Architecturesyllabus.cs.manchester.ac.uk/.../lectures/stack2.pdf · Lecture 10: Methods – Parameters and Variables Dr. Javier Navaridas School of Computer

Lecture 10: You are here

An Example Java Program

Lifespan of Variables in a Running Java Program

COMP15111 Lecture 10 An Example Java Program 2/33

Page 3: COMP15111: Introduction to Architecturesyllabus.cs.manchester.ac.uk/.../lectures/stack2.pdf · Lecture 10: Methods – Parameters and Variables Dr. Javier Navaridas School of Computer

An example Java Program – AgeHistory2

static int presentDay, presentMonth, presentYear;

void printAgeHistory (int person, birthDay, birthMonth, birthYear)int someYear, ageInSomeYear1;

System.out.println(...);... birthYear ... presentYear ... someYear ...

void main(String[] args)int birthDay1, birthMonth1, birthYear1, ...... = Integer.parseInt(args[...]);printAgeHistory(...);

void println(String string)print(string);

write(newline);

void print(String string)write(nextchar);

COMP15111 Lecture 10 An Example Java Program 4/33

Page 4: COMP15111: Introduction to Architecturesyllabus.cs.manchester.ac.uk/.../lectures/stack2.pdf · Lecture 10: Methods – Parameters and Variables Dr. Javier Navaridas School of Computer

An example Java Program – AgeHistory2

static int presentDay, presentMonth, presentYear;

void printAgeHistory (int person, birthDay, birthMonth, birthYear)int someYear, ageInSomeYear1;

System.out.println(...);... birthYear ... presentYear ... someYear ...

void main(String[] args)int birthDay1, birthMonth1, birthYear1, ...... = Integer.parseInt(args[...]);printAgeHistory(...);

void println(String string)print(string);

write(newline);

void print(String string)write(nextchar);

COMP15111 Lecture 10 An Example Java Program 4/33

Page 5: COMP15111: Introduction to Architecturesyllabus.cs.manchester.ac.uk/.../lectures/stack2.pdf · Lecture 10: Methods – Parameters and Variables Dr. Javier Navaridas School of Computer

An example Java Program – AgeHistory2

static int presentDay, presentMonth, presentYear;

void printAgeHistory (int person, birthDay, birthMonth, birthYear)int someYear, ageInSomeYear1;

System.out.println(...);... birthYear ... presentYear ... someYear ...

void main(String[] args)int birthDay1, birthMonth1, birthYear1, ...... = Integer.parseInt(args[...]);printAgeHistory(...);

void println(String string)print(string);

write(newline);

void print(String string)write(nextchar);

COMP15111 Lecture 10 An Example Java Program 4/33

Page 6: COMP15111: Introduction to Architecturesyllabus.cs.manchester.ac.uk/.../lectures/stack2.pdf · Lecture 10: Methods – Parameters and Variables Dr. Javier Navaridas School of Computer

An example Java Program – AgeHistory2

static int presentDay, presentMonth, presentYear;

void printAgeHistory (int person, birthDay, birthMonth, birthYear)int someYear, ageInSomeYear1;

System.out.println(...);... birthYear ... presentYear ... someYear ...

void main(String[] args)int birthDay1, birthMonth1, birthYear1, ...... = Integer.parseInt(args[...]);printAgeHistory(...);

void println(String string)print(string);

write(newline);

void print(String string)write(nextchar);

COMP15111 Lecture 10 An Example Java Program 4/33

Page 7: COMP15111: Introduction to Architecturesyllabus.cs.manchester.ac.uk/.../lectures/stack2.pdf · Lecture 10: Methods – Parameters and Variables Dr. Javier Navaridas School of Computer

An example Java Program – AgeHistory2

static int presentDay, presentMonth, presentYear;

void printAgeHistory (int person, birthDay, birthMonth, birthYear)int someYear, ageInSomeYear1;

System.out.println(...);... birthYear ... presentYear ... someYear ...

void main(String[] args)int birthDay1, birthMonth1, birthYear1, ...... = Integer.parseInt(args[...]);printAgeHistory(...);

void println(String string)print(string);

write(newline);

void print(String string)write(nextchar);

COMP15111 Lecture 10 An Example Java Program 4/33

Page 8: COMP15111: Introduction to Architecturesyllabus.cs.manchester.ac.uk/.../lectures/stack2.pdf · Lecture 10: Methods – Parameters and Variables Dr. Javier Navaridas School of Computer

Lecture 10: You are here

An Example Java Program

Lifespan of Variables in a Running Java Program

COMP15111 Lecture 10 Lifespan of Variables in a Running Java Program 5/33

Page 9: COMP15111: Introduction to Architecturesyllabus.cs.manchester.ac.uk/.../lectures/stack2.pdf · Lecture 10: Methods – Parameters and Variables Dr. Javier Navaridas School of Computer

Running AgeHistory2

birthYear1 etc.

presentYear etc.

main starts

COMP15111 Lecture 10 Lifespan of Variables in a Running Java Program 7/33

Page 10: COMP15111: Introduction to Architecturesyllabus.cs.manchester.ac.uk/.../lectures/stack2.pdf · Lecture 10: Methods – Parameters and Variables Dr. Javier Navaridas School of Computer

Running AgeHistory2

intString etc.

birthYear1 etc.

presentYear etc.

main calls parseInt (*6)

COMP15111 Lecture 10 Lifespan of Variables in a Running Java Program 9/33

Page 11: COMP15111: Introduction to Architecturesyllabus.cs.manchester.ac.uk/.../lectures/stack2.pdf · Lecture 10: Methods – Parameters and Variables Dr. Javier Navaridas School of Computer

Running AgeHistory2

birthYear1 etc.

presentYear etc.

parseInt returns to main

COMP15111 Lecture 10 Lifespan of Variables in a Running Java Program 11/33

Page 12: COMP15111: Introduction to Architecturesyllabus.cs.manchester.ac.uk/.../lectures/stack2.pdf · Lecture 10: Methods – Parameters and Variables Dr. Javier Navaridas School of Computer

Running AgeHistory2

someYear etc.

birthYear1 etc.

presentYear etc.

main calls printAgeHistory (*2)

COMP15111 Lecture 10 Lifespan of Variables in a Running Java Program 13/33

Page 13: COMP15111: Introduction to Architecturesyllabus.cs.manchester.ac.uk/.../lectures/stack2.pdf · Lecture 10: Methods – Parameters and Variables Dr. Javier Navaridas School of Computer

Running AgeHistory2

string etc.

someYear etc.

birthYear1 etc.

presentYear etc.

printAgeHistory calls println (*3?)

COMP15111 Lecture 10 Lifespan of Variables in a Running Java Program 15/33

Page 14: COMP15111: Introduction to Architecturesyllabus.cs.manchester.ac.uk/.../lectures/stack2.pdf · Lecture 10: Methods – Parameters and Variables Dr. Javier Navaridas School of Computer

Running AgeHistory2

string etc.

string etc.

someYear etc.

birthYear1 etc.

presentYear etc.

println calls print

COMP15111 Lecture 10 Lifespan of Variables in a Running Java Program 17/33

Page 15: COMP15111: Introduction to Architecturesyllabus.cs.manchester.ac.uk/.../lectures/stack2.pdf · Lecture 10: Methods – Parameters and Variables Dr. Javier Navaridas School of Computer

Running AgeHistory2

char etc.

string etc.

string etc.

someYear etc.

birthYear1 etc.

presentYear etc.

print calls write (*n)

COMP15111 Lecture 10 Lifespan of Variables in a Running Java Program 19/33

Page 16: COMP15111: Introduction to Architecturesyllabus.cs.manchester.ac.uk/.../lectures/stack2.pdf · Lecture 10: Methods – Parameters and Variables Dr. Javier Navaridas School of Computer

Running AgeHistory2

string etc.

string etc.

someYear etc.

birthYear1 etc.

presentYear etc.

write returns to print

COMP15111 Lecture 10 Lifespan of Variables in a Running Java Program 21/33

Page 17: COMP15111: Introduction to Architecturesyllabus.cs.manchester.ac.uk/.../lectures/stack2.pdf · Lecture 10: Methods – Parameters and Variables Dr. Javier Navaridas School of Computer

Running AgeHistory2

string etc.

someYear etc.

birthYear1 etc.

presentYear etc.

print returns to println

COMP15111 Lecture 10 Lifespan of Variables in a Running Java Program 23/33

Page 18: COMP15111: Introduction to Architecturesyllabus.cs.manchester.ac.uk/.../lectures/stack2.pdf · Lecture 10: Methods – Parameters and Variables Dr. Javier Navaridas School of Computer

Running AgeHistory2

char etc.

string etc.

someYear etc.

birthYear1 etc.

presentYear etc.

println calls write

COMP15111 Lecture 10 Lifespan of Variables in a Running Java Program 25/33

Page 19: COMP15111: Introduction to Architecturesyllabus.cs.manchester.ac.uk/.../lectures/stack2.pdf · Lecture 10: Methods – Parameters and Variables Dr. Javier Navaridas School of Computer

Running AgeHistory2

string etc.

someYear etc.

birthYear1 etc.

presentYear etc.

write returns to println

COMP15111 Lecture 10 Lifespan of Variables in a Running Java Program 27/33

Page 20: COMP15111: Introduction to Architecturesyllabus.cs.manchester.ac.uk/.../lectures/stack2.pdf · Lecture 10: Methods – Parameters and Variables Dr. Javier Navaridas School of Computer

Running AgeHistory2

someYear etc.

birthYear1 etc.

presentYear etc.

println returns to printAgeHistory

COMP15111 Lecture 10 Lifespan of Variables in a Running Java Program 29/33

Page 21: COMP15111: Introduction to Architecturesyllabus.cs.manchester.ac.uk/.../lectures/stack2.pdf · Lecture 10: Methods – Parameters and Variables Dr. Javier Navaridas School of Computer

Running AgeHistory2

birthYear1 etc.

presentYear etc.

printAgeHistory returns to main

COMP15111 Lecture 10 Lifespan of Variables in a Running Java Program 31/33

Page 22: COMP15111: Introduction to Architecturesyllabus.cs.manchester.ac.uk/.../lectures/stack2.pdf · Lecture 10: Methods – Parameters and Variables Dr. Javier Navaridas School of Computer

Running AgeHistory2

presentYear etc.

main finishes

COMP15111 Lecture 10 Lifespan of Variables in a Running Java Program 33/33