Methods: functions & procedures. Top-down programming Divide-and-conquer technique Divide-and-conquer technique Problem is broken down into a series of.

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

JavaScript I. JavaScript is an object oriented programming language used to add interactivity to web pages. Different from Java, even though bears some.
Procedural programming in Java
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 6 Functions.
// Functions that take no arguments #include using namespace std; void function1(); void function2( void ); int main() { function1(); function2(); return.
COMP 14 Introduction to Programming Miguel A. Otaduy May 25, 2004.
Overview creating your own functions calling your own functions.
COMP 110 Introduction to Programming Mr. Joshua Stough October 8, 2007.
1 More About Methods in Java CSC 1401: Introduction to Programming with Java Week 7 – Lecture 3 Wanda M. Kunkle.
COMP 14 Introduction to Programming Mr. Joshua Stough February 28, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 8, 2005.
CS 117 Spring 2002 Review for Exam 2 March 6, 2002 open book, 1 page of notes.
PHYS 2020 Making Choices; Arrays. Arrays  An array is very much like a matrix.  In the C language, an array is a collection of variables, all of the.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 6: Functions by.
Lesson 6 Functions Also called Methods CS 1 Lesson 6 -- John Cole1.
Parameters, Arguments, Local Variables, and Scope CSC 1401: Introduction to Programming with Java Week 8 – Lecture 1 Wanda M. Kunkle.
Agenda Review Compiling Review Data Types Integer Division Composition C++ Mathematical Functions User Input Reading: , 8.11 Homework #3.
Comp 248 Introduction to Programming Chapter 4 - Defining Classes Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
1 Methods Instructor: Mainak Chaudhuri
1 Chapter 9 Scope, Lifetime, and More on Functions.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 6 Functions.
Copyright © 2012 Pearson Education, Inc. Chapter 6: Functions.
Chapter 6: Functions Starting Out with C++ Early Objects
Chapter 06 (Part I) Functions and an Introduction to Recursion.
CSE 131 Computer Science 1 Module 1: (basics of Java)
Functions CIS Feb-06. Summary Slide Using Functions Mathematical Functions Misc. Functions Naming Conventions Writing Functions –Function Prototype.
Section 4 - Functions. All of the programs that we have studied so far have consisted of a single function, main(). However, having more than one function.
Procedural programming in Java Methods, parameters and return values.
User Defined Functions Chapter 7 2 Chapter Topics Void Functions Without Parameters Void Functions With Parameters Reference Parameters Value and Reference.
1 FUNCTIONS - I Chapter 5 Functions help us write more complex programs.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
“Education is a Treasure that follows you everywhere.” – Chines Proverb Methods and Functions.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 6 Functions.
Starting Out with C++ Early Objects ~~ 7 th Edition by Tony Gaddis, Judy Walters, Godfrey Muganda Modified for CMPS 1044 Midwestern State University 6-1.
1 Brief Version of Starting Out with C++, 4th Brief Edition Chapter 6 Functions.
Chapter 5 Classes and Methods II Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E.
More work with functions. Recall the “diary encoding problem”: (We shifted letters of the alphabet one position to the left.) – So IBM becomes HAL. –
Functions: Part 2 of /11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park 1.
Chapter Functions 6. Modular Programming 6.1 Modular Programming Modular programming: breaking a program up into smaller, manageable functions or modules.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Brief Edition Chapter 6 Functions.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6: Functions Starting Out with C++ Early Objects Eighth Edition.
Developed at Sun Microsystems in 1991 James Gosling, initially named “OAK” Formally announced java in 1995 Object oriented and cant write procedural.
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
Computer Science I Variables. Methods. Classwork/Homework: Re-do or do new drawing using variables & function(s). Zip folder and upload to Moodle dropbox.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 6: Functions.
CPSC 233 Tutorial 5 February 9 th /10 th, Java Classes Each Java class contains a set of instance variables and methods Instance Variables: Type.
Chapter 6 Functions. 6-2 Topics 6.1 Modular Programming 6.2 Defining and Calling Functions 6.3 Function Prototypes 6.4 Sending Data into a Function 6.5.
Functions Chapter 5. Function A set of instructions that are designed to perform specific task. A complete and independent program. It is executed by.
Checking character case class characterCase { public static void main (String arg[]) { char c = ‘A’; if (isUpperCase(c)) { System.out.println(c + “ is.
Eastside Robotics Alliance / Newport Robotics Group 1 T/Th, 6:30 – 8:30 PM Big Picture School Day 3 · 10/9/2014.
Lecture 12: Dividing Up Work. Why Using Functions Divide-and-conquer making large program development more manageable. Software reusability Use existing.
A little more JavaScript??. Building a caculator We'll need to learn about the following: – Form buttons. – Applying CSS rules by attribute values. –
Functions + Overloading + Scope
Programming what is C++
4. Java language basics: Function
A bit of C programming Lecture 3 Uli Raich.
Computer Science I Variables. Methods.
Functions, Part 2 of 2 Topics Functions That Return a Value
Methods The real power of an object-oriented programming language takes place when you start to manipulate objects. A method defines an action that allows.
Methods and Parameters
Method Mark and Lyubo.
An Introduction to Java – Part I, language basics
6 Chapter Functions.
CS2011 Introduction to Programming I Methods (I)
Chap 1 Chap 2 Chap 3 Chap 5 Surprise Me
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Classes, Objects and Methods
Agenda Types and identifiers Practice Assignment Keywords in Java
Standard Version of Starting Out with C++, 4th Edition
Presentation transcript:

Methods: functions & procedures

Top-down programming Divide-and-conquer technique Divide-and-conquer technique Problem is broken down into a series of smaller problems which are solved. Problem is broken down into a series of smaller problems which are solved.

Lemma (from lemma = a short theorem used in proving a larger theorem The late mathematician P. Erdos has often been associated with the observation that “a mathematician is a machine for converting coffee into theorems.” The late mathematician P. Erdos has often been associated with the observation that “a mathematician is a machine for converting coffee into theorems.” However, this characterization appears to be due to his friend, Alfred Rényi. However, this characterization appears to be due to his friend, Alfred Rényi. This thought was developed further by Erdos' friend and Hungarian mathematician Paul Turán, who suggested that weak coffee was suitable “only for lemmas.” This thought was developed further by Erdos' friend and Hungarian mathematician Paul Turán, who suggested that weak coffee was suitable “only for lemmas.”

Program composition mechanisms 1. Structured statements if, for, switch, while if, for, switch, while

Program composition mechanisms 2. Data structures Simple (atomic) Simple (atomic) boolean, int, double, float, char boolean, int, double, float, char Objects Objects Scanner, System, Math, String Scanner, System, Math, String Text files Text files

Program composition mechanisms 3. Methods Procedures Procedures Do work but don’t return anything. Do work but don’t return anything. Functions Functions Do work and return some result. Do work and return some result. AKA routines, subroutines, etc. AKA routines, subroutines, etc.

Problem Say we wish to write a function of our own. Say we wish to write a function of our own. It should return true if a given character is lower case, and false otherwise. It should return true if a given character is lower case, and false otherwise.

How would we do this in one of our programs now (w/out a function)? We could use an ‘if’ or a ‘switch.’ We could use an ‘if’ or a ‘switch.’

How would we do this in one of our programs now (w/out a function)? We could use an ‘if.’ We could use an ‘if.’ boolean isLC = false; if (ch=='a' || ch=='b' || … || ch=='z') isLC = true;

Why use a function at all? W/out function we would have to cut/copy/paste the code to everywhere we wish to use the code. If the variable names at that point in your code change (and they probably will), then you will have to change the names after the paste. W/out function we would have to cut/copy/paste the code to everywhere we wish to use the code. If the variable names at that point in your code change (and they probably will), then you will have to change the names after the paste. If the function is long, your programs will dramatically increase in size. If the function is long, your programs will dramatically increase in size. If we make a mistake, we’ll have to correct the mistake in many different places. If we make a mistake, we’ll have to correct the mistake in many different places. Just imagine if instead of pressing the sine button on our calculator, we have to press 1000 buttons and do the actual calculations! Just imagine if instead of pressing the sine button on our calculator, we have to press 1000 buttons and do the actual calculations!

Steps to define your own function. 1. Decide on a name for your function. 2. Determine what arguments your function needs to do its job. 3. Determine the return type of your function. 4. Code your function.

Steps to define your own function. 1. Decide on a name for your function. 2. Determine what arguments your function needs to do its job. 3. Determine the return type of your function. 4. Code your function. Before we code our own function, how would we apply these steps to create something familiar like sine?

Back to our original problem Say we wish to write a function of our own. Say we wish to write a function of our own. It should return true if a given character is lower case, and false otherwise. It should return true if a given character is lower case, and false otherwise.

Steps to define your own function. Step 1. Decide on a name. isLowerCase

Steps to define your own function. Step 2. Determine what arguments your functions needs to do its job. What does isLowerCase need to do its job? What does isLowerCase need to do its job? 1. One character to check. ch ch

After steps 1 and 2. isLowerCase ( char ch ) { …} Step 3. What type of thing does our function return?

After steps 1, 2, and 3. static boolean isLowerCase ( char ch ) { …} Now all we have to do is fill in the body of our functions.

Step 4. Fill in the body of the function. static boolean isLowerCase ( char ch ) {…} Now all we have to do is fill in the body of our functions. Recall that a l.c. character is one that is in the interval [‘a’.. ‘z’].

Step 4. Fill in the body of the function. static boolean isLowerCase ( char ch ) { //use a switch } Now all we have to do is fill in the body of our functions. Recall that a l.c. character is one that is in the interval [‘a’.. ‘z’].

Step 4. Fill in the body of the function. static boolean isLowerCase ( char ch ) { //use a switch boolean isLC = false; switch (ch) { case 'a' : case 'b' : … case 'z' : isLC = true; break;} return isLC; }

Step 4. Fill in the body of the function. static boolean isLowerCase ( char ch ) { //use a switch switch (ch) { case 'a' : case 'b' : … case 'z' : return true; break; default : return false; break;}}

Step 4. Fill in the body of the function. static boolean isLowerCase ( char ch ) { //use an if } Now all we have to do is fill in the body of our functions. Recall that a l.c. character is one that is in the interval [‘a’.. ‘z’].

Step 4. Fill in the body of the function. static boolean isLowerCase ( char ch ) { //use an if if (ch=='a' || ch=='b' || … || ch=='z') return true; return false; } Any other way to use an if?

Step 4. Fill in the body of the function. static boolean isLowerCase ( char ch ) { //use an if if (ch 'z') return false; return true; }

Step 4. Fill in the body of the function. static boolean isLowerCase ( char ch ) { //just use a return }

Step 4. Fill in the body of the function. static boolean isLowerCase ( char ch ) {//minimal return ('a'<=ch && ch<='z'); }

Using our function if (isLowerCase('c')) … if (isLowerCase(c1)) … if (isLowerCase(c1) || isLowerCase(c2)) …

Architecture of a function

Definition of function static boolean isUpperCase (char p ) { if ('A'<=p && p<='Z')return true; return false; }

Function heading or signature (or prototype) static boolean isUpperCase (char p ) { if ('A'<=p && p<='Z')return true; return false; }

Function body static boolean isUpperCase (char p ) { if ('A'<=p && p<='Z')return true; return false; }

Function parts static boolean isUpperCase (char p ) { if ('A'<=p && p<='Z')return true; return false; } Function type Function name Formal parameters (function parameters)

Formal parameters static boolean isUpperCase ( char p ) { if ('A'<=p && p<='Z')return true; return false; } Formal parameters 0 or more of them 0 or more of them Local variables Local variables Hold values of actual parameters Hold values of actual parameters

What happens when a function is called/used/invoked? 1. Formal parameter(s) is created as a new variable. New variable is initialized to actual parameter value. 2. Execute function body. Return function value. 3. Program continues after function designator (invocation).

More functions

Say we wish to define a function that calculates the square of a number. 1. What should we call it? 2. What does it need (formal parameters) to do the calculation? 3. What types of numbers does it need? 4. How will we give back the result? Does it give back a result? Does it give back a result? 5. What is the type of the result (if any)? We use the keyword void to indicate that we will not return anything. We use the keyword void to indicate that we will not return anything.

Say we wish to define a function that calculates the square of a number. 1. What should we call it?

Say we wish to define a function that calculates the square of a number. 1. What should we call it? square

Say we wish to define a function that calculates the square of a number. 2. What does it need (formal parameters) to do the calculation?

Say we wish to define a function that calculates the square of a number. 2. What does it need (formal parameters) to do the calculation? A number to square.

Say we wish to define a function that calculates the square of a number. 3. What types of numbers does it need?

Say we wish to define a function that calculates the square of a number. 3. What types of numbers does it need? double

Say we wish to define a function that calculates the square of a number. After steps 1 to 3, we have: square ( double x )

Say we wish to define a function that calculates the square of a number. 4. How will we give back the result? Does it give back a result? Does it give back a result?

Say we wish to define a function that calculates the square of a number. 4. How will we give back the result? Does it give back a result? Does it give back a result? Yes. Yes.

Say we wish to define a function that calculates the square of a number. 5. What is the type of the result (if any)? We use the keyword void to indicate that we will not return anything. We use the keyword void to indicate that we will not return anything.

Say we wish to define a function that calculates the square of a number. 5. What is the type of the result (if any)? We use the keyword void to indicate that we will not return anything. We use the keyword void to indicate that we will not return anything. Double Double So now we have: static double square ( double x ) { …}

Say we wish to define a function that calculates the square of a number. static double square ( double x ) { //let’s define the body of the function. }

Say we wish to define a function that calculates the square of a number. static double square ( double x ) { double result = x*x; return result; } Did you write this in your notes?

Areas from geometry A square = length * length A rectangle = width * height A triangle = ½ * base * height A parallelogram = base * height Can you define functions that perform each of these calculations?