Methods & Functions.

Slides:



Advertisements
Similar presentations
Execute Blocks of Code Multiple Times Svetlin Nakov Telerik Corporation
Advertisements

Execute Blocks of Code Multiple Times Telerik Software Academy C# Fundamentals – Part 1.
Intro Programming By Trevor Decker Team 1743 By Trevor Decker Team 1743.
Μαθαίνοντας Python [Κ4] ‘Guess the Number’
Professor John Peterson
CS 280 Data Structures Professor John Peterson. Big O Notation We use a mathematical notation called “Big O” to talk about the performance of an algorithm.
CSC 142 J 1 CSC 142 Arrays [Reading: chapter 10].
Python quick start guide
Grouping objects Collections and iterators. Main concepts to be covered Collections Loops Iterators.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
CSCI 130 Chapter 5 Functions. Functions are named uniquely Performs a specific task Is independent –should not interfere with other parts of program May.
How to Create a Videogame By: Connor McCann. Java Java is one of many programming languages Java is used to run web browsers and most PC video games I.
More arrays Primitive vs. reference parameters. Arrays as parameters to functions.
Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.
Advanced Computer Science Lesson 4: Reviewing Loops and Arrays Reading User Input.
Two-Dimensional Arrays That’s 2-D Arrays Girls & Boys! One-Dimensional Arrays on Steroids!
AP Computer Science edition Review 1 ArrayListsWhile loopsString MethodsMethodsErrors
More loops while and do-while. Recall the for loop in general for (initialization; boolean_expression; update) { }
Counting Loops.
A: A: double “4” A: “34” 4.
Application development with Java Lecture 6 Rina Zviel-Girshin.
Creating and Using Class Methods. Definition Class Object.
REPETITION STATEMENTS - Part1  Also called LOOP STATEMENTS OR LOOP STRUCTURES 1 C++ Statements that repeat one or more actions while some condition is.
Test Review. General Info. All tests will be comprehensive. You will be tested more on your understanding of code as opposed to your ability to write.
Data Structures Arrays and Lists Part 2 More List Operations.
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
Chapter 7 Control Structures. Java has very flexible three looping mechanisms. You can use one of the following three loops:  while Loop  do...while.
ITM 3521 ITM 352 Functions. ITM 3522 Functions  A function is a named block of code (i.e. within {}'s) that performs a specific set of statements  It.
 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays Outline 1 Introduction 2 Arrays 3Declaring Arrays 4Processing Array Contents 5 Multiple-Subscripted.
Loops Review. How to generate random numbers Math.random() will return a random decimal value in the form of a double. double num1 = Math.random(); num1.
A 2-D Array is a structure that storage space both vertically and horizontally. Thus, the array has both rows and columns. 2-D Arrays are used to create.
CS1010 Discussion Group 11 Week 5 – Functions, Selection, Repetition.
CIS199 Test Review 2 REACH.
Chapter VII: Arrays.
CSC 211 Java I for loops and arrays.
Information and Computer Sciences University of Hawaii, Manoa
Chapter 11 - JavaScript: Arrays
CS 160 – Summer 16 Exam 1 Prep.
4. Java language basics: Function
IGCSE 4 Cambridge Data types and arrays Computer Science Section 2
Two Dimensional Array Mr. Jacobs.
Alg2_1c Extra Material for Alg2_1
CMPT 201 Functions.
AP Search and Sort Review
Engineering Innovation Center
An Introduction to Java – Part I
Using variables, for..loop and functions to help organize your code
An Introduction to Java – Part I, language basics
Arrays We often want to organize objects or primitive data in a way that makes them easy to access and change. An array is simple but powerful way to.
160 Exam 2 Prep.
Object Oriented Programming in java
Logical Operations In Matlab.
CS2011 Introduction to Programming I Arrays (I)
Code Refresher Test #1 Topics:
Loops.
2/24/2019.
CSC 142 Arrays [Reading: chapter 12].
Arrays October 6, 2006 ComS 207: Programming I (in Java)
Arrays in Java.
Suggested self-checks: Section 7.11 #1-11
Fundamental Programming
Peer Instruction 4 Control Loops.
Statements in C Programming
Question 1a) What is printed by the following Java program? int s;
Comparing Python and Java
IPC144 Introduction to Programming Using C Week 5 – Lesson 1
Methods/Functions.
Introduction to java Part I By Shenglan Zhang.
ITM 352 Functions.
Lesson 3. Controlling program flow. Loops. Methods. Arrays.
Presentation transcript:

Methods & Functions

Methods A function is a code block containing a series of statements that are focused on producing a single result Functions differ from methods in that they return a value. The value type is predefined in the declaration of the function. Every function must contain at least one return statement outside of a conditional

int[] searchArray = new int[50]; //Create a new array of size 50 //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - void setup() { size(500,500); if( initArray(searchArray)){ find(50); //call find functions to find certain values find(12); find(76); } boolean initArray(int[] s) for(int j=0; j < s.length; j++) s[j] = random(1,100); //initialize array to random numbers return true; void find(int searchKey) boolean isFound = false; //set a 'flag' to see if we found the value for (int i = 0; i < searchArray.Length; i++) //loop through entire array values if (searchKey == searchArray[i]) //check for a match text("Found “ + str(searchKey) + ” at " + str(i), 100, 100); //write to console index of found value isFound = true; if(!isFound) //write out msg to console if we didn't find the value text("Sorry, could not find " + str(searchKey) + " in array", 100, 100); }// find

Methods vs Functions Method Function int squareANumber(int number) No return type Method name Parameter(s) void squareANumber(int number) { int square = number * number; text(str(square),100, 100); } int squareANumber(int number) return number * number; Function Integer return type Function name Parameter(s) Return statement

Functions - return statement At least one return statement must exist outside of a conditional int getValue() { if(!blocked) return -1; } return value;

Task 1. Write a small program that declares an int array called grades. This array will be of size 10 integer grade numbers (between 0 and 100) 2. Write a second array of strings called students. Initialize this second array manually with 10 names (“last, first”) of your choice. For the program write a method or function: a) void init() that initializes grades to random integers. b) void print() that writes the names of the students and their grade c) void set(int grade, String student) that sets a certain student’s grade to a specified grade d) int get(String student) that gets and returns a certain student’s grade e) void sort() that sorts the grades from lowest to highest