Functions Day 1 karelpart 4functions1 1 Functions Functions

  • Slides: 11
Download presentation
Functions Day 1 karel_part 4_functions_1 1

Functions Day 1 karel_part 4_functions_1 1

Functions • Functions return values or Objects. – Using a function allows the programmer

Functions • Functions return values or Objects. – Using a function allows the programmer to focus on other task. – Using a function allows the programmer to leave the details till later or someone else. karel_part 4_functions_1 2

Functions return – a value • • – An Object • A Robot •

Functions return – a value • • – An Object • A Robot • A Direction • Penguin Number Boolean value Words Direction – nothing – State of an Object • Height • Number of beepers karel_part 4_functions_1 3

Functions return a value • Functions that do NOT return a value are called

Functions return a value • Functions that do NOT return a value are called procedures karel_part 4_functions_1 4

Functions • Functions may calculate a value, e. g. – Circumference – Volume –

Functions • Functions may calculate a value, e. g. – Circumference – Volume – Distance between Objects – Elapse Time – Etc. karel_part 4_functions_1 5

Functions • Functions may return a known value, e. g. – Color – size

Functions • Functions may return a known value, e. g. – Color – size karel_part 4_functions_1 6

User Define Function • In java (or Karel) new function can be created! public

User Define Function • In java (or Karel) new function can be created! public return. Type method. Name(parameters list) – return. Type indicate what is being returned by this function. – If return. Type is void, the function returns nothing and is called a procedure. – method. Name is the name of the function. – parameters list is any additional information needed to return the desired results! • Parameter list may be empty or contain pairs of words indicating the type (number, word, etc and its reference) karel_part 4_functions_1 7

Example • The following method has two parameters, s 1 and s 2 and

Example • The following method has two parameters, s 1 and s 2 and returns one string that is built by concatenating s 1 & s 2 public String combine(String s 1, String s 2) { String str = s 1 + s 2; return str; } Sample call String ans = combine(“Computer”, “Science”); Sets ans to “Computer. Science” karel_part 4_functions_1 8

Another Example • The following method has three parameters, s 1, s 2 and

Another Example • The following method has three parameters, s 1, s 2 and s 3 and returns the sum s 1 + s 2 + s 3 public int sum(int s 1, int s 2, int s 3) { int ans = s 1 + s 2 + s 3; return ans; } Sample call int ans = sum(3, 20, 100); Sets ans to 123 int ans = sum(-13, 5); Sets ans to 5 karel_part 4_functions_1 9

Another Example • The following method has three parameters, s 1, s 2 and

Another Example • The following method has three parameters, s 1, s 2 and s 3 and returns the sum s 1 + s 2 + s 3 public int sum(int s 1, int s 2, int s 3) { return s 1 + s 2 + s 3; } Sample call int ans = sum(3, 20, 100); Sets ans to 123 int ans = sum(-10, -3, 0), 13, 5); Sets ans to 5 karel_part 4_functions_1 10

Homework #1 • Now would be a good time to do the first homework

Homework #1 • Now would be a good time to do the first homework assignment for this section! • Now is also a good time to start the function project karel_part 4_functions_1 11