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

Page - 34 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python

Image of the Page - 34 -

Image of the Page - 34 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python

Text of the Page - 34 -

34 2 BasicConstructions Notice the two returnvalueswhich are simply separated by a comma. Whencall- ing the function (andprinting), argumentsmust appear in the sameorder as in the functiondefinition.Wewould thenwrite print xy(initial_x_velocity, initial_y_velocity, time) The two returned values from the function could alternatively have been assigned tovariables, e.g., as x_pos, y_pos = xy(initial_x_velocity, initial_y_velocity, time) Thevariablesx_posandy_poscould thenhavebeenprintedorused inotherways in thecode. Therearepossibilities forhavingavariablenumberof functioninputandoutput parameters (using *args and **kwargs constructions for the arguments). How- ever,wedonotgo further into that topichere. Variables that are defined inside a function, e.g., g in the last xy function, are local variables. Thismeans they are only known inside the function. Therefore, if youhadaccidentallyusedg in somecalculationoutside the function,youwould havegotanerrormessage. Thevariabletime isdefinedoutside the functionand is therefore aglobal variable. It is knownbothoutside and inside the function(s). If youdefineoneglobalandone localvariable,bothwith thesamename, thefunction only sees the local one, so theglobalvariable isnot affectedbywhathappenswith the localvariableof the samename. The arguments named in the heading of a function definition are by rule local variables inside the function. If youwant to change the value of a global variable inside a function, you need to declare the variable as global inside the function. That is, if the global variablewasx, wewould need towriteglobal x inside the function definition beforewe let the function change it. After function execution, xwould then have a changed value. One should strive to define variablesmostly where theyareneededandnoteverywhere. Anotherveryusefulwayofhandlingfunctionparameters inPython, isbydefin- ingparameters as keyword arguments. This givesdefault values toparameters and allowsmore freedom in function calls, since the order and number of parameters mayvary. Letus illustrate theuseofkeywordargumentswith the functionxy. Assumewe definedxyas def xy(t, v0x=0, v0y=0): g = 9.81 # acceleration of gravity return v0x*t, v0y*t - 0.5*g*t**2 Here, t is an ordinary or positional argument, whereas v0x and v0y are keyword arguments or named arguments. Generally, there can be many positional argu- ments andmanykeywordarguments, but thepositional argumentsmustalwaysbe listed before the keyword arguments in function definition. Keyword arguments aregivendefault values, as shownherewithv0xandv0y, bothhavingzero asde-
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
Web-Books
Library
Privacy
Imprint
Austria-Forum
Austria-Forum
Web-Books
Programming for Computations – Python