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 - 86 -
  • User
  • Version
    • full version
    • text only version
  • Language
    • Deutsch - German
    • English

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

Image of the Page - 86 -

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

Text of the Page - 86 -

86 4 FunctionsandtheWritingofCode Alternatively, with default values for all the parameters, and again aiming for the sameprintout,ourprogram(ball_position_xy.py)appearsas def xy(v0x=2.0, v0y=5.0, t=0.6): """Computes horizontal and vertical positions at time t""" g = 9.81 # acceleration of gravity return v0x*t, v0y*t - 0.5*g*t**2 x, y = xy() print(’Horizontal position: {:g} , Vertical position: {:g}’.format(x, y)) Running thecode,we get theprintout Horizontal position: 1.2 , Vertical position: 1.2342 Some,orall, of thedefaultvaluesmayalsobeoverridden,e.g., like x, y = xy(v0y=6.0) # override default value for v0y only whichmeansthatxywill runwithdefaultvaluesforv0xandt,whilev0ywillhave thevalue6.0, as specified in the functioncall. Theoutput then,becomes Horizontal position: 1.2 , Vertical position: 1.8342 which is reasonable, since the initial velocity for the vertical direction was higher than in thepreviouscase. 4.1.8 AFunctionwithAnotherFunctionasInputArgument Functions are straightforwardly passed as arguments to other functions. This is illustrated by the following scriptfunction_as_argument.py,where a function sumsupfunctionvaluesofanother function: def f(x): return x def g(x): return x**2 def sum_function_values(f, start, stop): """Sum up function values for integer arguments as f(start) + f(start+1) + f(start+2) + ... + f(stop)""" S = 0 for i in range(start, stop+1, 1): S = S + f(i) return S print(’Sum with f becomes {:g}’.format(sum_function_values(f, 1, 3))) print(’Sum with g becomes {:g}’.format(sum_function_values(g, 1, 3))) We note that the function sum_function_values takes a function as its first argument and repeatedly calls that function without (of course) knowing what that functiondoes, it justgets the functionvaluesbackandsums themup! Remember that the argument f in the function header sum_funcion_values (f, start, stop) and the function defined by the namef, is not the same. The
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