Q and A for Chapter 6 CS 104 Victor Norman. Return values Q: A function definition that returns a value (i.e., a non-void function) must have at least.

Slides:



Advertisements
Similar presentations
Escape Sequences \n newline \t tab \b backspace \r carriage return
Advertisements

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.
Python Programming Chapter 5: Fruitful Functions Saad Bani Mohammad Department of Computer Science Al al-Bayt University 1 st 2011/2012.
Computer Programming w/ Eng. Applications
12. Common Errors, a few Puzzles. © O. Nierstrasz P2 — Common Errors, a few Puzzles 12.2 Common Errors, a few Puzzles Sources  Cay Horstmann, Computing.
Q and A for Chapter 3.4 – 3.14 CS 104 Victor Norman.
Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
Chapter 6 Horstmann Programs that make decisions: the IF command.
CS 106 Introduction to Computer Science I 01 / 30 / 2008 Instructor: Michael Eckmann.
Selection Statements choice of one among several blocks of code Java supports 3 kinds of selection statements: if statement – selects one block or leaves.
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
© The McGraw-Hill Companies, 2006 Chapter 5 Arrays.
CS 106 Introduction to Computer Science I 02 / 28 / 2007 Instructor: Michael Eckmann.
CS 117 Spring 2002 Review for Exam 2 March 6, 2002 open book, 1 page of notes.
Flow control 1: if-statements (Liang 72-80) if(radius < 0) { System.out.println(“cannot get area: radius below zero”); } else { double area = radius *
Introduction to Programming G51PRG University of Nottingham Revision 2 Essam Eliwa.
TODAY’S LECTURE Review Chapter 2 Go over exercises.
Control Structures - Repetition Chapter 5 2 Chapter Topics Why Is Repetition Needed The Repetition Structure Counter Controlled Loops Sentinel Controlled.
11 Chapter 4 LOOPS AND FILES. 22 THE INCREMENT AND DECREMENT OPERATORS To increment a variable means to increase its value by one. To decrement a variable.
Intro to Generic Programming Templates and Vectors.
COMP More About Classes Yi Hong May 22, 2015.
CS61A Lecture 2 Functions and Applicative Model of Computation Tom Magrino and Jon Kotker UC Berkeley EECS June 19, 2012.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
1 Conditions Logical Expressions Selection Control Structures Chapter 5.
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.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Templates An introduction. Simple Template Functions template T max(T x, T y) { if (x > y) { return x; } else { return y; } } int main(void) { int x =
Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to.
PHP Conditional Statements Conditional statements in PHP are used to perform different actions based on different conditions. Conditional Statements Very.
1Computer Sciences Department Princess Nourah bint Abdulrahman University.
Session Three Review & Conditional Control Flow. Java File Hierarchy Projects Packages Classes Methods.
Copyright © 2012 Pearson Education, Inc. Chapter 6 More Conditionals and Loops Java Software Solutions Foundations of Program Design Seventh Edition John.
Q and A for Sections 2.9, 4.1 Victor Norman CS106 Fall 2015.
CSC 110 Using Python [Reading: chapter 1] CSC 110 B 1.
1 FUNCTIONS - I Chapter 5 Functions help us write more complex programs.
Chapter 10 Defining Classes. The Internal Structure of Classes and Objects Object – collection of data and operations, in which the data can be accessed.
C++ Classes and Data Structures Jeffrey S. Childs
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
Functions Overview Functions are sequence of statements with its own local variables supports modularity, reduces code duplication Data transfer between.
Functions Victor Norman CS104 Calvin College. Reading Quiz Counts toward your grade.
 In computer programming, a loop is a sequence of instruction s that is continually repeated until a certain condition is reached.  PHP Loops :  In.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
CS 106 Introduction to Computer Science I 02 / 01 / 2008 Instructor: Michael Eckmann.
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
1 1 Additional Control Structures Chapter 9 2 New and Improved... Ways to branch Ways to write loops Understanding the break and continue statements.
Language Find the latest version of this document at
+ Storage Classes and Linkage. + Introduction Scope describe the region or regions of a program that can access and identifier Variables can be shared.
1 Scope Lifetime Functions (the Sequel) Chapter 8.
Week 10 - Wednesday.  What did we talk about last time?  Method example  Roulette simulation  Types in Java.
Functions Chapter 4 Python for Informatics: Exploring Information Slightly modified by Recep Kaya Göktaş on March 2015.
1 Review for Midterm 2 Aaron Bloomfield CS 101-E.
Announcements Assignment 2 Out Today Quiz today - so I need to shut up at 4:25 1.
Winter 2016CISC101 - Prof. McLeod1 CISC101 Reminders Quiz 3 next week. See next slide. Both versions of assignment 3 are posted. Due today.
Learning Javascript From Mr Saem
CS0007: Introduction to Computer Programming Primitive Data Types and Arithmetic Operations.
CONDITIONALS CITS1001. Scope of this lecture if statements switch statements Source ppts: Objects First with Java - A Practical Introduction using BlueJ,
Eastside Robotics Alliance / Newport Robotics Group 1 T/Th, 6:30 – 8:30 PM Big Picture School Day 3 · 10/9/2014.
Session Three Review & Conditional Control Flow. Java File Hierarchy Projects Packages Classes Methods.
CSE 374 Programming Concepts & Tools
IF statements.
If, else, elif.
CISC101 Reminders Assn 3 due tomorrow, 7pm.
Recap Week 2 and 3.
IAT 800 Foundations of Computational Art and Design
LOOPS The loop is the control structure we use to specify that a statement or group of statements is to be repeatedly executed. Java provides three kinds.
CS 1054: Lecture 2, Chapter 1 Objects and Classes.
Loops CGS3416 Spring 2019 Lecture 7.
Methods/Functions.
SEEM 4540 Tutorial 4 Basic PHP based on w3Schools
Presentation transcript:

Q and A for Chapter 6 CS 104 Victor Norman

Return values Q: A function definition that returns a value (i.e., a non-void function) must have at least one of what kind of statement? A: return statement like this return somevalue

Return vs. print Q: Why do you have to return a value? Why not just print it? A: Suppose sin(x) doesn’t return the value, but prints it only. How would you do this?: sin(x) ** 2 cos(sin(x)) Printed values are sent out to the screen, but cannot be used in later computations.

Multiple return statements Q: When do you need to have multiple return statements in a function definition? A: When your entire code is an if-else block, then each “branch” of the conditional must have a return statement. NOTE: always best for all returns to return a value of the same type.

Return’s meanings Q: What 2 things does a return statement do? A: 1. Specifies the value to be returned 2. Makes the function return immediately. Code below it is not run. NOTE: you cannot put return in the main code. It must be in a function definition.

Dead code Q and A: Identify the dead code below: def computeIQ(yrsInSchool, dadsIQ, momsIQ): if dadsIQ == 0: iq = yrsInSchool * momsIQ return iq dadsIQ = momsIQ else: return (dadsIQ + momsIQ) * yrsInSchool return 100.0# return default IQ.

Return multiple values? Q: Can a function return multiple values at once? A: No. A function can return only one thing. But, it could be a thing that holds multiple values, like a list or a tuple…

What happens to a returned value? Q: What happens to a returned value? Does it just get printed? A: The returned value must be stored in a variable or printed out explicitly. Doing this: y = sin(x) stores the result in y. Doing this: sin(x) does nothing (in script mode): the returned value is lost.

Incremental Development Q: In the text, the author talks about incremental development. What does he mean by this? A: He means writing new code 1 line at a time (sometimes), and testing your code each time. It helps the n00b write better code faster.

Boolean function practice 1 Q: Write a function isIsosceles() that takes 3 floats as parameters and returns True if two of the side lengths are the same, and False otherwise. A: def isIsosceles(s1, s2, s3): return (s1 == s2 or s1 == s3 or s2 == s3)

Boolean function practice 2 Q: Write a function isScalene() that takes 3 floats as parameters and returns True if no two sides have the same length. A: def isScalene(s1, s2, s3): return (s1 != s2 and s1 != s3 and s2 != s3)

Boolean function practice 3 Q: Write a function isEquilateral() that takes 3 floats as parameters and returns True if all sides have the same length. A: def isEquilateral(s1, s2, s3): return (s1 == s2 == s3)

Boolean function practice 4 Q: Write a function isNotEquilateral() that calls isEquilateral() but returns True if the 3 sides are all not the same. A: def isNotEquilateral(s1, s2, s3): return not isEquilateral(s1, s2, s3)

“Leap of Faith” section Q: What does the author mean by this “leap of faith”? I don’t get it. A: The author means that when checking code, you just assume a call to a function returns or does the right thing – you don’t examine the code thoroughly every time. You can do this when designing code, too: just write code with function calls that you haven’t implemented yet, and then implement them later.

isinstance() Q: I don’t understand this isinstance() stuff… A: That’s not a question… It takes two parameters: isinstance(variablename, typename) and returns a Boolean value (True or False) Useful for defensive programming: checking to see if args to a function are of the correct type.