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

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

Image of the Page - 81 -

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

Text of the Page - 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.
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