Download presentation
Presentation is loading. Please wait.
Published byวินัย วอชิงตัน Modified over 5 years ago
1
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.
2
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
3
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
4
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
5
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) ?
6
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) ?
7
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) ?
8
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
9
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* g(-4/2) g x = -4 >>> demo(-4) ? return -1 * -4 4
10
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) ?
11
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* g(-4//2) g x = -2 >>> demo(-4) ? return -1 * -2 2
12
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* >>> demo(-4) ?
13
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* 46 >>> demo(-4) ?
14
42 How functions work… >>> demo(-4) 42 def demo(x): demo
return x + f(x) demo x = -4 return def f(x): return 11*g(x) + g(x//2) def g(x): return -1.0 * x >>> demo(-4) 42 42
15
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* 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…
16
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 …
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.