More Functions with Return Values CS303E: Elements of Computers and Programming.

Slides:



Advertisements
Similar presentations
Chapter 7 Introduction to Procedures. So far, all programs written in such way that all subtasks are integrated in one single large program. There is.
Advertisements

CS107 Introduction to Computer Science Lecture 3, 4 An Introduction to Algorithms: Loops.
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
Mathematical Operators  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming in.
Basic Input/Output and Variables Ethan Cerami New York
CSC 8310 Programming Languages Meeting 2 September 2/3, 2014.
More Functions CS303E: Elements of Computers and Programming.
Lecture 15 Practice & exploration Subfunctions: Functions within functions “Guessing Game”: A hands-on exercise in evolutionary design © 2007 Daniel Valentine.
JS Arrays, Functions, Events Week 5 INFM 603. Agenda Arrays Functions Event-Driven Programming.
REPETITION STRUCTURES. Topics Introduction to Repetition Structures The while Loop: a Condition- Controlled Loop The for Loop: a Count-Controlled Loop.
Introduction to Python
Lists and More About Strings CS303E: Elements of Computers and Programming.
Munster Programming Training
1 CSC 221: Introduction to Programming Fall 2012 Functions & Modules  standard modules: math, random  Python documentation, help  user-defined functions,
Methods. 2 A sequence of statements can be packaged together as a unit and re-used. A method is a named unit of re-usable code. modifier returnType methodName(
 Expression Tree and Objects 1. Elements of Python  Literals, Strings, Tuples, Lists, …  The order of file reading  The order of execution 2.
COMPUTER PROGRAMMING. Functions What is a function? A function is a group of statements that is executed when it is called from some point of the program.
1 CS Programming Languages Class 15 October 17, 2000.
More While Loop Examples CS303E: Elements of Computers and Programming.
Practice with Lists and Strings CS303E: Elements of Computers and Programming.
Announcements Project1 Due Wednesday September 21 st 4:30pm We will post instructions on how to turnin from home Note: you can always turn in from the.
NA2204.1jcmt CSE 1320 Intermediate Programming C Program Basics Structure of a program and a function type name (parameters) { /* declarations */ statement;
Chapter 8 More On Functions. "The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. First cut, scope.
1 while loops. 2 Definite loops definite loop: A loop that executes a known number of times.  The for loops we have seen so far are definite loops. We.
Python Functions.
Introduction to Computer Programming
1 Computer Science of Graphics and Games MONT 105S, Spring 2009 Lecture 17 Parameters, Scope, Return values.
End of unit assessment Challenge 1 & 2. Course summary So far in this course you have learnt about and used: Syntax Output to screen (PRINT) Variables.
Conditional Loops CSIS 1595: Fundamentals of Programming and Problem Solving 1.
Introduction to Python Dr. José M. Reyes Álamo. 2 Three Rules of Programming Rule 1: Think before you program Rule 2: A program is a human-readable set.
Loops and Simple Functions CS303E: Elements of Computers and Programming.
INLS 560 – F UNCTIONS Instructor: Jason Carter.
An Introduction to Programming with C++ Sixth Edition Chapter 10 Void Functions.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 5 Repetition Structures.
CSC 1010 Programming for All Lecture 4 Loops Some material based on material from Marty Stepp, Instructor, University of Washington.
Data Types and Conversions, Input from the Keyboard CS303E: Elements of Computers and Programming.
Programming Fundamentals I Java Programming Spring 2009 Instructor: Xuan Tung Hoang TA: Tran Minh Trung Lab 03.
1 CSE 142 Midterm Review Problems These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They may not be rehosted, sold, or modified.
1 CS 177 Week 6 Recitation Slides Review for Midterm Exam.
Lecture 2 Functions. Functions in C++ long factorial(int n) The return type is long. That means the function will return a long integer to the calling.
Function and Function call Functions name programs Functions can be defined: def myFunction( ): function body (indented) Functions can be called: myFunction(
Functions CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Loops and Simple Functions COSC Review: While Loops Typically used when the number of times the loop will execute is indefinite Typically used when.
PYTHON FUNCTIONS. What you should already know Example: f(x) = 2 * x f(3) = 6 f(3)  using f() 3 is the x input parameter 6 is the output of f(3)
Designing Functions CSIS 1595: Fundamentals of Programming and Problem Solving 1.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Functions with Arguments and Return Values, Oh My! CS303E: Elements of Computers and Programming.
Review: A Structural View program modules -> main program -> functions -> libraries statements -> simple statements -> compound statements expressions.
Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Third Edition by Tony Gaddis.
Chapter 9: Value-Returning Functions
1-1 Logic and Syntax A computer program is a solution to a problem.
Chapter 10: Void Functions
Functions CIS 40 – Introduction to Programming in Python
Midterm Review Problems
Topics Introduction to Repetition Structures
Programming Right from the Start with Visual Basic .NET 1/e
Passing Parameters by value
Functions Pass By Value Pass by Reference
BBIT 212/ CISY 111 Object Oriented Programming (OOP)
MAT 4830 Mathematical Modeling
G. Pullaiah College of Engineering and Technology
Recursion Chapter 11.
Introduction to C++ Programming Language
CS 1111 Introduction to Programming Spring 2019
四時讀書樂 (春) ~ 翁森 山光照檻水繞廊,舞雩歸詠春風香。 好鳥枝頭亦朋友,落花水面皆文章。 蹉跎莫遣韶光老,人生唯有讀書好。
Functions John R. Woodward.
Using Modules.
Presentation transcript:

More Functions with Return Values CS303E: Elements of Computers and Programming

Announcements Exam 2: Wednesday, April 3 rd Exam 2: Wednesday, April 3 rd During regular class time – NOT in this classroom. During regular class time – NOT in this classroom. Location will be posted on course webpage. Location will be posted on course webpage.

Reminder: Variable Scope and Functions The scope of a variable is the part of the program in which that variable is accessible The scope of a variable is the part of the program in which that variable is accessible A function parameter’s scope is its function A function parameter’s scope is its function

Pass By Value A function cannot change the value of the argument that is passed to it A function cannot change the value of the argument that is passed to it –The value of the argument in the calling statement does not change, no matter how the function manipulates the parameter Some programming languages do this, others use pass by reference Some programming languages do this, others use pass by reference –It is important to find out which!

Pass By Value: Example def main(): arg1=35 arg1=35 print “arg1 is”, arg1 print “arg1 is”, arg1 changeIt(arg1) changeIt(arg1) print “after call,” +\ “arg1 is”, arg1 print “after call,” +\ “arg1 is”, arg1 def changeIt(par): #recall that initially #recall that initially #par is set to the #value of arg1 #par is set to the #value of arg1 par=-1 par=-1 print “par is”, par print “par is”, par What happens when this program executes?

Return By Value The same applies to return statements The same applies to return statements The calling program cannot modify the variable from the function The calling program cannot modify the variable from the function Typical in all programming languages Typical in all programming languages

Question What does pass by value mean? A. Changes to a function’s argument are reflected in the calling function B. Changes to a function’s argument are NOT reflected in the calling function

Returning Multiple Values A function may return a tuple which is a collection of values. A function may return a tuple which is a collection of values. –So… it’s not really multiple values, but one value that is itself a collection of values Example: return a,b return a,b But how do you assign multiple values?

Simultaneous Assignment Use simultaneous assignment to assign multiple values in one statement Use simultaneous assignment to assign multiple values in one statementa,b=2,3 print “a=%d b=%d” % (a,b) Output: a=2 b=3

Returning Multiple Values: Example def divide(a,b) #return dividend and remainder in a tuple #return dividend and remainder in a tuple return a/b, a%b return a/b, a%b def main(): div,rem=divide(13,8) div,rem=divide(13,8) print “%d/%d=%d R %d” % (13,8,div,rem) print “%d/%d=%d R %d” % (13,8,div,rem)

Question: What are the values of a and b? What are the values of a and b? def add_sub(x,y): return x+y,x-y return x+y,x-y def main(): a=4 a=4 b=5 b=5 a,b=add_sub(a,b) a,b=add_sub(a,b) A. a=4 b=5 B. a=9 b=9 C. a=9 b=-1 D. a=-1 b=9

Exercise Write a function minMax() that takes 2 arguments, a and b, and returns a tuple containing the minimum and mximum of a and b, in that order. Then write a main function that calls minMax() on all combinations of inputs a and b such that 1<=a<=5, 0<=b<=3.

Group Exercise Write a program to simulate a digital clock. Your program should print the values of each second like this: 00:00:0000:00:0100:00:02…23:59:59 You will need the following code: import time time.sleep(1) #tells #program to wait for 1 #second before continuing

Group Exercise Write a program that quizzes the user on addition until the user enters “done”. Your program should generate two random numbers between 1 and 1,000 and print them to the screen (like this: a + b = ) and then allow the user to enter an answer. The program should report whether the answer is right or wrong and give the correct answer if it is wrong. You’ll need: random.randint(1,1000)