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

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

Bild der Seite - 79 -

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

Text der Seite - 79 -

4FunctionsandtheWritingofCode 4.1 Functions:HowtoWriteThem? Untilnow,wehavebenefittedfromusing functions like,e.g.,zerosandlinspace from numpy. These have all been written by others for us to use. Now we will look at how to write such functions ourselves, an absolutely fundamental skill in programming. In its simplest form, a function in a program is much like a mathematical function:someinputnumberx is transformedtosomeoutputnumber.Oneexample is the tanh−1(x) function, calledatan in computer code: it takes one real number as inputand returnsanothernumber.Functions in Pythonare moregeneral andcan take a series of values as input and return one or more results, or simply nothing. Thepurposeof functions is twofold: 1. to group code lines that naturally belong together (making such units of code is a strategywhichmayease theproblemsolvingprocessdramatically),and 2. to parameterize a set of code lines such that they can be written only once and easilybe re-executedwithvariations. Functions that we write ourselves are often referred to as user-defined functions. Throughout this book,we will present many examples, in variouscontexts, of how such functionsmaybewritten. 4.1.1 Example:WritingOurFirstFunction To grasp the first few essentials about function writing, we changeball.py from Sect.1.2, so that the heighty is rathercomputedbyuse of a function that wedefine ourselves.Also, to betterdemonstrate theuse ofour function,we computeyat two pointsintime(notonlyone,asinball.py).Thenewprogramball_function.py thenappearsas def y(v0, t): g = 9.81 # Acceleration of gravity ©The Author(s) 2020 S.Linge, H.P. Langtangen, Programming forComputations -Python, Texts in Computational Science and Engineering 15, https://doi.org/10.1007/978-3-030-16877-3_4 79
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