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

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

Image of the Page - 84 -

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

Text of the Page - 84 -

84 4 FunctionsandtheWritingofCode is5ms−1. Inourmainprogram,we could then try twoalternativecalls, print(y(5, 0.6)) # works fine print(y(0.6, 5)) # gives no error message, but the wrong result! Here,y is called with positionalargumentsonly (also termedordinaryarguments). The first alternative will do the job. Regarding the second alternative, it will print a result, but a wrong one, since argumentsare in the wrong position when calling. With the second call to y (i.e., y(0.6, 5)), we get v0 = 0.6 and t = 5 in the function, which clearly is not what we intended. Note that Python has no way of knowing that this is wrong, so it will happily compute a height according to the functiondefinitionandprintananswer. Doing the samewithvariables, e.g., like initial_velocity = 5 time = 0.6 print(y(initial_velocity, time)) # works fine print(y(time, initial_velocity)) # No error message, but wrong result! will of course not change anything. The first call will give the correct result, while the latterwill print thewrongresult (withoutanyerrormessage). UsingKeywordsintheCall It ispossibletousefunctioninputparameternamesas keywordswhen calling the function (note, the functiondefinition is still unchanged withonlypositionalparameters!).Thisbrings theadvantageofmakingthefunction callmorereadable.Also, it allows theorderofargumentstobeswitched,according tosomerules.Afew exampleswill illustratehowthisworks. By use of the parameter namesv0 and t from the function definition, we may have the followingstatements in main: print(y(v0=5, t=0.6)) # works fine print(y(t=0.6, v0=5)) # order switched, works fine with keywords! Here, y is called with keyword arguments only (also termed named arguments). Eitherof the twoalternativecallswillgivethecorrectprintout,since, irrespectiveof argumentordering,Python is explicitly told which function parameter should have which value. This generalizes: as long as keywords are used for all arguments in a functioncall, anyorderingof argumentscanbeused. It is allowed to have a mix of positional and keyword arguments in the call, however, but then we need to be a bit more careful. With the following lines in main, v0 = 5 print(y(v0, t=0.6)) # works fine print(y(t=0.6, v0)) # gives syntax error! the first alternative is acceptable, while the second is not. The general rule is, that when mixing positional and keyword arguments, all the positional arguments must precede the keyword arguments. The positional argumentsmust also come in the very same order as the corresponding input parameters appear in the function definition,while theorderof thekeywordargumentsmaybechanged. Notethat, ifsomefunctionisdefinedwitha“long”listof inputparameters,when calling that function, you can not use a keyword for one of the arguments “in the middle”,evenifplaced last in the list (makeyourselfanexamplewith3parameters
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