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

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

Image of the Page - 79 -

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

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