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

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

Image of the Page - 83 -

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

Text of the Page - 83 -

4.1 Functions:HowtoWriteThem? 83 4.1.4 LocalVersusGlobalVariables In our program ball_function.py, we have defined the variable g inside the function y. This makes g a local variable, meaning that it is only known inside the function. Thus, if we had tried to usegoutside of the function, we would have got an error message. To see this, we may insertprint(v0*t - 0.5*g*t**2)as a new last line in main (i.e., after the last print(y(v0, time))) and try to run the code. Now, Python will not recognizeg, even if Python just used it inside the function y (you should try this to see for yourself, but remember to remove the insertedcodeafter the test!). The variables v0 and time are defined outside the function and are therefore globalvariables.Theyareknownbothoutsideand inside the function.4 Inputparameters listed in a functionheaderare by rule localvariables inside the function. If you define one global and one local variable, both with the same name (asv0 inball_function.py), the functionbody only“sees” the local one, so the globalvariable isnotaffectedbywhathappens to its local“name-brother”. Ifyouwant tochangethevalueofaglobalvariableinsideafunction,youneedto declarethevariableasglobalinsidethefunction.Thatis, ifsomeglobalvariablewas namedx,wewouldneedtowriteglobal x insidethefunctiondefinitionbeforewe let the function change it. After function execution,xwould then have a changed value. 4.1.5 CallingaFunctionDefinedwithPositionalParameters We will here discuss alternative ways of calling the function y from ballnrea_function.py. This function has only got positional parameters, and throughout, the definition def y(v0, t): g = 9.81 return v0*t - 0.5*g*t**2 iskeptunchanged. Parameter Versus Argument As explained previously, the input variables speci- fied ina functiondefinitionarecalled theparametersof the function.Whenwecall that function,however, thevaluesprovidedin thecallare referredtoasarguments.5 MixingthePositionofArguments Assumenow,likebefore, thatwejustwantour mainprogramto compute the heightof theball after0.6s,when the initialvelocity 4 The observant reader may then ask why not just define the functionywithout input parameters v0andt, andsimplyusev0andtimedirectly inside thefunction(changingt totime in thereturn statement)? The answer is that this would have worked. However, there are several issues related to the use ofglobal variables, so it is something that should be avoided in general. 5 https://docs.python.org/3.3/faq/programming.html#faq-argument-vs-parameter.
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