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

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

Bild der Seite - 83 -

Bild der Seite - 83 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Band Second Edition

Text der Seite - 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.
zurück zum  Buch Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Band Second Edition"
Programming for Computations – Python A Gentle Introduction to Numerical Simulations with Python 3.6, Band Second Edition
Titel
Programming for Computations – Python
Untertitel
A Gentle Introduction to Numerical Simulations with Python 3.6
Band
Second Edition
Autoren
Svein Linge
Hans Petter Langtangen
Verlag
Springer Open
Datum
2020
Sprache
englisch
Lizenz
CC BY 4.0
ISBN
978-3-319-32428-9
Abmessungen
17.8 x 25.4 cm
Seiten
356
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