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

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

Bild der Seite - 35 -

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

Text der Seite - 35 -

2.2 Functions 35 fault value. Ina script, the functionxymaynowbecalled inmanydifferentways. For example, print xy(0.6) wouldmakexyperformthecomputationswitht = 0.6and thedefaultvalues (i.e zero)ofv0xandv0y. The twonumbers returned fromxyareprinted to thescreen. Ifwewanted touseanother initial value forv0y,wecould, e.g.,write print xy(0.6,v0y=4.0) whichwouldmake xy perform the calculationswith t = 0.6, v0x = 0 (i.e. the default value) andv0y = 4.0. When there are several positional arguments, they have to appear in the same order as defined in the function definition, unless we explicitlyuse thenamesof thesealso in the functioncall.Withexplicit namespec- ification in the call, anyorder of parameters is acceptable. To illustrate,we could, e.g., callxyas print xy(v0y=4.0, v0x=1.0, t=0.6) In any programming language, it is a good habit to include a little explanation ofwhat the function is doing, unlesswhat is doneby the function is obvious, e.g., whenhavingonly a fewsimple code lines. This explanation is called adoc string, which inPython shouldbeplaced just at the topof the function. This explanation ismeant forahumanwhowants tounderstand thecode, so it shouldsaysomething about thepurposeof thecodeandpossiblyexplain theargumentsandreturnvalues if needed. Ifwe do thatwith our xy function fromabove, wemaywrite the first linesof the functionas def xy(v0x, v0y, t): """Compute the x and y position of the ball at time t""" Notethatotherfunctionsmaybecalledfromwithinotherfunctions,andfunction input parameters are not required to be numbers. Any objectwill do, e.g., string variablesorother functions. Functionsarestraightforwardlypassedasarguments toother functions, as illus- tratedby the followingscriptfunction_as_argument.py: def sum_xy(x, y): return x + y def prod_xy(x, y): return x*y def treat_xy(f, x, y): return f(x, y) x = 2; y = 3 print treat_xy(sum_xy, x, y) print treat_xy(prod_xy, x, y)
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