Page - 32 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python
Image of the Page - 32 -
Text of the Page - 32 -
32 2 BasicConstructions
will be understoodbyPython asfirst compute the expression, then send the result
back (i.e., return) to where the functionwascalled from. Bothdefandreturnare
reservedwords. Thefunctiondependsont, i.e.,onevariable(orwesaythat it takes
oneargument or input parameter), the valueofwhichmust be providedwhen the
function is called.
What actually happenswhen Pythonmeets this code? The def line just tells
Python thathere is a functionwithnameyand it hasoneargumentt. Pythondoes
not look into the function at this stage (except that it checks the code for syntax
errors). WhenPython later onmeets the statementprint y(time), it recognizes
a function cally(time) and recalls that there is a functiony definedwith one ar-
gument. The value of time is then transferred to the y(t) function such that t
= timebecomes thefirst action in they function. ThenPython executes one line
at a time in the y function. In the final line, the arithmetic expression v0*t -
0.5*g*t**2 is computed, resulting in a number, and this number (ormore pre-
cisely, thePythonobject representing thenumber)replaces thecally(time) in the
callingcodesuchthat thewordprintnowprecedesanumberratherthanafunction
call.
Pythonproceedswith thenext lineandsetstime toanewvalue. Thenextprint
statement triggers a new call to y(t), this time t is set to 0.9, the computations
are done line by line in the y function, and the returned result replaces y(time).
Insteadofwritingprint y(time),wecouldalternativelyhavestored thereturned
result fromthey function inavariable,
h = y(time)
print h
Note thatwhena functioncontainsif-elif-elseconstructions,returnmay
bedone fromwithin anyof thebranches. Thismaybe illustratedby the following
functioncontaining threereturnstatements:
def check_sign(x):
if x > 0:
return ’x is positive’
elif x < 0:
return ’x is negative’
else:
return ’x is zero’
Remember that only one of the branches is executed for a single call on check_
sign, so depending on the number x, the returnmay take place from any of the
three returnalternatives.
Toreturnattheendornot
Programmersdisagreewhether it is agood idea tousereturn insidea function
where youwant, or if there should only be one singlereturn statement at the
endof the function. Theauthorsof thisbookemphasize readablecodeand think
thatreturncanbeuseful inbranchesas intheexampleabovewhenthefunction
is short. For longer ormore complicated functions, it might be better to have
back to the
book Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python"
Programming for Computations – Python
A Gentle Introduction to Numerical Simulations with Python
- Title
- Programming for Computations – Python
- Subtitle
- A Gentle Introduction to Numerical Simulations with Python
- Authors
- Svein Linge
- Hans Petter Langtangen
- Publisher
- Springer Open
- Date
- 2016
- Language
- English
- License
- CC BY-NC 4.0
- ISBN
- 978-3-319-32428-9
- Size
- 17.8 x 25.4 cm
- Pages
- 248
- Keywords
- Programmiersprache, Informatik, programming language, functional, imperative, object-oriented, reflective
- Category
- Informatik