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

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

Image of the Page - 35 -

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

Text of the Page - 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)
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