Web-Books
in the Austria-Forum
Austria-Forum
Web-Books
Informatik
Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Volume Second Edition
Page - 80 -
  • User
  • Version
    • full version
    • text only version
  • Language
    • Deutsch - German
    • English

Page - 80 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Volume Second Edition

Image of the Page - 80 -

Image of the Page - 80 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Volume Second Edition

Text of the Page - 80 -

80 4 FunctionsandtheWritingofCode return v0*t - 0.5*g*t**2 v0 = 5 # Initial velocity time = 0.6 # Just pick one point in time print(y(v0, time)) time = 0.9 # Pick another point in time print(y(v0, time)) The Function Definition When Python reads this program from the top, it takes the code from the line withdef, to the line with return, to be the definition of a function by the namey. Note that this function (or any function for that matter) is notexecuteduntil it iscalled.Here, thathappenswith the twocalls (y(v0, time)) appearing in the print commands further down in the code. In other words, when Python reads the function definition for the first time, it just “stores the definition for lateruse” (andchecks for syntaxerrors, see Exercise4.8). CallingtheFunction Whenthefunctioniscalledthefirst time, thevaluesofv0(5) andtime (0.6) are transferred to they function such that, in the function,v0 = 5 andt = 0.6.Thereafter,Pythonexecutes the function lineby line. In thefinal line return v0*t - 0.5*g*t**2, theexpressionv0*t - 0.5*g*t**2 is computed, resulting in a number which is “returned” to replacey(v0, time) in the calling code. The function print is then called with this number as input argument and proceedstoprint it (i.e., theheight).Alternatively,thenumberreturnedfromycould have been assigned to a variable, e.g., like h = y(v0, time), before printing as print(h). Python then proceeds with the next line, setting time to 0.9, before a new function call is triggered in the following line, causing a second execution of the functionyandprintoutof thenewheight. VariableNames in FunctionCalls Observe that,whencalling the functiony, the time was contained in the variabletime, whereas the corresponding input variable (called a parameter) in the function y had the name t. We should reveal already now, that in general, variable names in function calls do not have to be the same as the corresponding names in the function definition. However, with v0 here, we see that variable names can be the same in the function call and the function definition(but technically, theyare thentwodifferentvariableswith thesamename, seeSect.4.1.4).More rulesabout thiswill followsoon. 4.1.2 CharacteristicsofaFunctionDefinition Function Structure We may now write up a more general form of a Python functionas def function_name(p1, p2, p3, ...): # function header """This is a docstring""" # ...in function body <code line> # ...in function body <code line> # ...in function body
back to the  book Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Volume Second Edition"
Programming for Computations – Python A Gentle Introduction to Numerical Simulations with Python 3.6, Volume Second Edition
Title
Programming for Computations – Python
Subtitle
A Gentle Introduction to Numerical Simulations with Python 3.6
Volume
Second Edition
Authors
Svein Linge
Hans Petter Langtangen
Publisher
Springer Open
Date
2020
Language
English
License
CC BY 4.0
ISBN
978-3-319-32428-9
Size
17.8 x 25.4 cm
Pages
356
Keywords
Programmiersprache, Informatik, programming language, functional, imperative, object-oriented, reflective
Category
Informatik
Web-Books
Library
Privacy
Imprint
Austria-Forum
Austria-Forum
Web-Books
Programming for Computations – Python