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 - 81 -
  • Benutzer
  • Version
    • Vollversion
    • Textversion
  • Sprache
    • Deutsch
    • English - Englisch

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

Bild der Seite - 81 -

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

Text der Seite - 81 -

4.1 Functions:HowtoWriteThem? 81 ... ... return result_1, result_2, ... # last line in function body # First line after function definition Afunctiondefinitionmustappearbeforethefunctioniscalled,andusually,function definitions are placed one after the other at the top of a program, after import statements (if any). FunctionHeader(PositionalParameters) Thefirst line,oftencalled the function header, always starts with the reserved word def. This word is succeeded by the name of the function, followed by a listing of comma-separated names in parentheses, being the parameters of the function (here symbolized with p1, p2, p3,...).These parameters specify what input the functionneeds forexecution.The header always ends with a colon. Function and parameter names must be decided by the programmer. The function header may contain any number1 of parameters, also none. When there are no parameters, the parentheses must still be there, i.e., like def function_name():. When input parameters are listed on the form above, they are referred to as positionalparameters.Below,inSect.4.1.5,wewillseewhatfreedomthereiswhen callingfunctionsdefinedwith suchparameters. Another Typical Function Header (Keyword Parameters) Another kind of function header that you often will deal with, is one which allows default values to begiven to some,or all, of the inputparameters.We may write sucha headeron the following form2 (it could replace the previous header that has only positional parameters): def function_name(p1, p2, p3=default_3, p4=default_4, ...): In this case, the two first parameters are positional, whereasp3 andp4 are known as keyword parameters. This means that, unless other values are specified for p3 and p4 when calling the function, they will get the default values default_3 and default_4, respectively. This will soon (Sect.4.1.7) be demonstrated and discussed inmoredetail throughexamples. A function header may have only positional parameters, only keyword parame- ters, or some of each. However,positional parametersmust always be listed before keywordparameters (as shownin theheaderabove). Note that there should be no space to each side of =when specifying keyword parameters. Function Body All code lines inside a function must be indented, conventionally with 4 spaces, or more (if there are more indentation levels). In a function, these indented lines represent a block of statements, collectively referred to as the function body. Once the indent is reversed, we are outside (and after) the function. 1 It is possible to have a variable number of input parameters (using *args and **kwargs). However, we do not pursue that any further here. 2 Strictly speaking, this header also incorporates the previous header, but they were split just to clarify the presentation.
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