Web-Books
im Austria-Forum
Austria-Forum
Web-Books
Informatik
Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python
Seite - 34 -
  • Benutzer
  • Version
    • Vollversion
    • Textversion
  • Sprache
    • Deutsch
    • English - Englisch

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

Bild der Seite - 34 -

Bild der Seite - 34 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python

Text der Seite - 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-
zurück zum  Buch Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python"
Programming for Computations – Python A Gentle Introduction to Numerical Simulations with Python
Titel
Programming for Computations – Python
Untertitel
A Gentle Introduction to Numerical Simulations with Python
Autoren
Svein Linge
Hans Petter Langtangen
Verlag
Springer Open
Datum
2016
Sprache
englisch
Lizenz
CC BY-NC 4.0
ISBN
978-3-319-32428-9
Abmessungen
17.8 x 25.4 cm
Seiten
248
Schlagwörter
Programmiersprache, Informatik, programming language, functional, imperative, object-oriented, reflective
Kategorie
Informatik
Web-Books
Bibliothek
Datenschutz
Impressum
Austria-Forum
Austria-Forum
Web-Books
Programming for Computations – Python