Problem to solve Given four numbers, days, hours, minutes, and seconds, compute the total number of seconds that is equivalent to them. For example, 0.

Slides:



Advertisements
Similar presentations
Parameter passing mechanism: pass-by-reference. The Pass-by-reference mechanism - the agreement Recall: Parameter passing mechanism = agreement between.
Advertisements

Write a program step by step. Step 1: Problem definition. Given the coordinate of two points in 2-D space, compute and print their straight distance.
Def f(n): if (n == 0): return else: print(“*”) return f(n-1) f(3)
Python Programming Language
Chapter 6: User-Defined Functions I
 To be able to write larger programs ◦ By breaking them down into smaller parts and passing data between the parts.  To understand the concepts of Methods.
Python quick start guide
chipKit Sense Switch & Control LED
Chapter 6: User-Defined Functions I Instructor: Mohammad Mojaddam
PYTHON. Python is a high-level, interpreted, interactive and object- oriented scripting language. Python was designed to be highly readable which uses.
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.
Hey, Ferb, I know what we’re gonna do today! Aims: Use formatted printing. Use the “while” loop. Understand functions. Objectives: All: Understand and.
Java Script User Defined Functions. Java Script  You can define your own JavaScript functions. Such functions are called user- defined, as opposed to.
Functions. Built-in functions You’ve used several functions already >>> len("ATGGTCA")‏ 7 >>> abs(-6)‏ 6 >>> float("3.1415")‏ >>>
Topics: Function Intitution Algorithm Example Function Definition (and Indentation) Function Call Naming and Binding Local Variables Function Arguments.
EECS 110: Lec 4: Functions and Recursion Aleksandar Kuzmanovic Northwestern University
Introduction to Computing Using Python for loop / def- ining a new function  Execution control structures ( if, for, function call)  def -ining a new.
CSC 110 Using Python [Reading: chapter 1] CSC 110 B 1.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
1 Structure of a C Program (continued) Presentation original from Dr. Turner’s class USF - COP C for Engineers Summer 2008.
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.
Python Functions : chapter 3
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
Xiaojuan Cai Computational Thinking 1 Lecture 6 Defining Functions Xiaojuan Cai (蔡小娟) Fall, 2015.
Memory Management in Java Computer Science 3 Gerb Objective: Understand references to composite types in Java.
Modularity using Functions Chapter 4. Modularity In programming blocks of code often can be "called up" and reused whenever necessary, for example code.
Chapter 3: User-Defined Functions I
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
EECS 110: Lec 9: Review for the Midterm Exam Aleksandar Kuzmanovic Northwestern University
The last CS 5 lecture you’ll ever need! Inducing labor for the machine! == Reducing labor for humans! On Warner Brothers' insistence, we affirm that this.
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)
First Program  Open a file  In Shell  Type into the file: 3  You did it!!! You wrote your first instruction, or code, in python!
Quiz 3 Topics Functions – using and writing. Lists: –operators used with lists. –keywords used with lists. –BIF’s used with lists. –list methods. Loops.
Functions with Arguments and Return Values, Oh My! CS303E: Elements of Computers and Programming.
Chapter 6 Functions The Tic-Tac-Toe Game. Chapter Content In this chapter you will learn to do the following: 0 Write your own functions 0 Accept values.
Functions. What is a Function?  We have already used a few functions. Can you give some examples?  Some functions take a comma-separated list of arguments.
ENGINEERING 1D04 Tutorial 1. WELCOME! What we’re doing today Who am I How to do well What are computer programs Software Development Cycle Pseudo Code.
Introduction to Programming
More on Functions (Part 2)
Chapter 6: User-Defined Functions I
Introduction to Programming
EECS 110: Lec 9: Review for the Midterm Exam
Topic: Functions – Part 2
Functions CIS 40 – Introduction to Programming in Python
EECS 110: Lec 4: Functions and Recursion
CMSC201 Computer Science I for Majors Lecture 10 – Functions (cont)
Procedures Programming Guides.
Programming In Lesson 3.
Passing Parameters by value
What does this do? def revList(L): if len(L) < = 1: return L x = L[0] LR = revList(L[1:]) return LR + x.
EECS 110: Lec 4: Functions and Recursion
Writing Functions( ) (Part 4)
Functions Christopher Harrison | Content Developer
Python 19 Mr. Husch.
Python Programming Language
Chapter 6: User-Defined Functions I
functions: argument, return value
Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg
COMPUTER PROGRAMMING SKILLS
Input Based Programs.
In this class, we will cover:
Introduction to Computer Science
Sec. 2.2 Functions.
General Computer Science for Engineers CISC 106 Lecture 03
Python Functions.
Python 19 Mr. Husch.
CSE 190p University of Washington Michael Ernst
def-ining a function A function as an execution control structure
Defining Functions.
Presentation transcript:

Problem to solve Given four numbers, days, hours, minutes, and seconds, compute the total number of seconds that is equivalent to them. For example, 0 days, 0 hours, 2 minutes, 3 seconds would give 123 seconds. Or 3600 seconds would equal to 0 days, 1 hours, 0 minutes, and 0 seconds.

How to tackle the problem? We can use a list to represent the days, hours, minutes, and seconds that are fed to the function. The function then should return as the result of computation the total number of seconds. We’d like the program run something like >>> convert_to_seconds([0, 0, 2, 3]) 123 >>> convert_to_seconds([0, 1, 0, 0]) 3600

input to function (parameters) How functions look… input to function (parameters) name def convert_to_seconds(in_list): """ convert_to_sectons(in_list): Converts a list of [days, hours, minutes, seconds] into seconds. Input: a list of four int’s """ seconds = in_list[3] # number of seconds minutes = in_list[2] # number of minutes hours = in_list[1] # number of hours days = in_list[0] # number of days return seconds + minutes*60 + hours*3600 + days*24*3600 docstring code block show help! Function can have several input parameters, not just one. For instance, def getMoneyString(dollars, cents): return “I have “ + str(dollars) + “ dollars ” + str(cents) + “ cents ” comments return statement

How functions work… What is demo(-4) ? def demo(x): return x + f(x) Assume you have written three functions: What is demo(-4) ? def demo(x): return x + f(x) def f(x): return 11*g(x) + g(x//2) Again, recall that before we call demo(-4) the functions demo, f and g just silently sit around waiting to be called into action. def g(x): return -1 * x

How functions work… >>> demo(-4) ? demo x = -4 def demo(x): return x + f(x) demo x = -4 return -4 + f(-4) def f(x): return 11*g(x) + g(x//2) def g(x): return -1 * x Now demo is actually executed. Important to know (write on the board): When Python comes to a function call, it initiates a four-step process: 1. The calling program suspends execution at the point of the call. 2. The parameters that are used by the function get assigned the values supplied for this call. 3. The body of the function is executed. 4. Control returns to the point just after where the function was called. >>> demo(-4) ?

How functions work… >>> demo(-4) ? def demo(x): demo return x + f(x) demo x = -4 return -4 + f(-4) def f(x): return 11*g(x) + g(x//2) f x = -4 def g(x): return -1 * x return 11*g(x) + g(x//2) >>> demo(-4) ?

How functions work… These are different x's ! >>> demo(-4) ? def demo(x): return x + f(x) demo x = -4 return -4 + f(-4) def f(x): return 11*g(x) + g(x//2) f x = -4 def g(x): return -1 * x return 11*g(x) + g(x//2) These are different x's ! >>> demo(-4) ?

How functions work… >>> demo(-4) ? def demo(x): demo return x + f(x) demo x = -4 return -4 + f(-4) def f(x): return 11*g(x) + g(x//2) f x = -4 def g(x): return -1 * x return 11*g(-4) + g(-4//2) g x = -4 >>> demo(-4) ? return -1 * x

How functions work… >>> demo(-4) ? def demo(x): return x + f(x) demo x = -4 return -4 + f(-4) def f(x): return 11*g(x) + g(x//2) f x = -4 def g(x): return -1 * x return 11* 4 + g(-4/2) g x = -4 >>> demo(-4) ? return -1 * -4 4

How functions work… >>> demo(-4) ? def demo(x): demo return x + f(x) demo x = -4 return -4 + f(-4) def f(x): return 11*g(x) + g(x//2) f x = -4 def g(x): return -1.0 * x return 11* 4 + g(-4//2) >>> demo(-4) ?

How functions work… >>> demo(-4) ? def demo(x): return x + f(x) demo x = -4 return -4 + f(-4) def f(x): return 11*g(x) + g(x//2) f x = -4 def g(x): return -1 * x return 11* 4 + g(-4//2) g x = -2 >>> demo(-4) ? return -1 * -2 2

How functions work… >>> demo(-4) ? def demo(x): demo return x + f(x) demo x = -4 return -4 + f(-4) def f(x): return 11*g(x) + g(x//2) f x = -4 def g(x): return -1.0 * x return 11* 4 + 2 >>> demo(-4) ?

How functions work… >>> demo(-4) ? def demo(x): demo return x + f(x) demo x = -4 return -4 + f(-4) def f(x): return 11*g(x) + g(x//2) f x = -4 def g(x): return -1.0 * x return 11* 4 + 2 46 >>> demo(-4) ?

42 How functions work… >>> demo(-4) 42 def demo(x): demo return x + f(x) demo x = -4 return -4 + 46 def f(x): return 11*g(x) + g(x//2) def g(x): return -1.0 * x >>> demo(-4) 42 42

the stack Function stacking def demo(x): return x + f(x) demo x = -4 the stack return -4 + f(-4) def f(x): return 11*g(x) + g(x//2) f x = -4 def g(x): return -1.0 * x return 11* 4 + g(-4//2) g x = -2 return -1 * -2 2 (1) keeps separate variables for each function call… (2) remembers where to send results back to…

return provides the function call's value … return != print def dbl(x): """ doubles x """ return 2*x def dblPR(x): """ doubles x """ print (2*x) >>> answer = dbl(21) >>> answer 42 >>> answer = dblPR(21) 42 >>> answer >>> print just prints! It does not return anything Print is for people Stress the importance of this difference! None is frequently used to represent the absence of a value What is answer in the second case? None. Every function returns something, even if the value is not specified explicitly return provides the function call's value …