4: Functions
- Page ID
- 122322
\( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)
\( \newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\)
( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\)
\( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)
\( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\)
\( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)
\( \newcommand{\Span}{\mathrm{span}}\)
\( \newcommand{\id}{\mathrm{id}}\)
\( \newcommand{\Span}{\mathrm{span}}\)
\( \newcommand{\kernel}{\mathrm{null}\,}\)
\( \newcommand{\range}{\mathrm{range}\,}\)
\( \newcommand{\RealPart}{\mathrm{Re}}\)
\( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)
\( \newcommand{\Argument}{\mathrm{Arg}}\)
\( \newcommand{\norm}[1]{\| #1 \|}\)
\( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)
\( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\AA}{\unicode[.8,0]{x212B}}\)
\( \newcommand{\vectorA}[1]{\vec{#1}} % arrow\)
\( \newcommand{\vectorAt}[1]{\vec{\text{#1}}} % arrow\)
\( \newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vectorC}[1]{\textbf{#1}} \)
\( \newcommand{\vectorD}[1]{\overrightarrow{#1}} \)
\( \newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}} \)
\( \newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}} \)
\( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)
\(\newcommand{\avec}{\mathbf a}\) \(\newcommand{\bvec}{\mathbf b}\) \(\newcommand{\cvec}{\mathbf c}\) \(\newcommand{\dvec}{\mathbf d}\) \(\newcommand{\dtil}{\widetilde{\mathbf d}}\) \(\newcommand{\evec}{\mathbf e}\) \(\newcommand{\fvec}{\mathbf f}\) \(\newcommand{\nvec}{\mathbf n}\) \(\newcommand{\pvec}{\mathbf p}\) \(\newcommand{\qvec}{\mathbf q}\) \(\newcommand{\svec}{\mathbf s}\) \(\newcommand{\tvec}{\mathbf t}\) \(\newcommand{\uvec}{\mathbf u}\) \(\newcommand{\vvec}{\mathbf v}\) \(\newcommand{\wvec}{\mathbf w}\) \(\newcommand{\xvec}{\mathbf x}\) \(\newcommand{\yvec}{\mathbf y}\) \(\newcommand{\zvec}{\mathbf z}\) \(\newcommand{\rvec}{\mathbf r}\) \(\newcommand{\mvec}{\mathbf m}\) \(\newcommand{\zerovec}{\mathbf 0}\) \(\newcommand{\onevec}{\mathbf 1}\) \(\newcommand{\real}{\mathbb R}\) \(\newcommand{\twovec}[2]{\left[\begin{array}{r}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\ctwovec}[2]{\left[\begin{array}{c}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\threevec}[3]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\cthreevec}[3]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\fourvec}[4]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\cfourvec}[4]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\fivevec}[5]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\cfivevec}[5]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\mattwo}[4]{\left[\begin{array}{rr}#1 \amp #2 \\ #3 \amp #4 \\ \end{array}\right]}\) \(\newcommand{\laspan}[1]{\text{Span}\{#1\}}\) \(\newcommand{\bcal}{\cal B}\) \(\newcommand{\ccal}{\cal C}\) \(\newcommand{\scal}{\cal S}\) \(\newcommand{\wcal}{\cal W}\) \(\newcommand{\ecal}{\cal E}\) \(\newcommand{\coords}[2]{\left\{#1\right\}_{#2}}\) \(\newcommand{\gray}[1]{\color{gray}{#1}}\) \(\newcommand{\lgray}[1]{\color{lightgray}{#1}}\) \(\newcommand{\rank}{\operatorname{rank}}\) \(\newcommand{\row}{\text{Row}}\) \(\newcommand{\col}{\text{Col}}\) \(\renewcommand{\row}{\text{Row}}\) \(\newcommand{\nul}{\text{Nul}}\) \(\newcommand{\var}{\text{Var}}\) \(\newcommand{\corr}{\text{corr}}\) \(\newcommand{\len}[1]{\left|#1\right|}\) \(\newcommand{\bbar}{\overline{\bvec}}\) \(\newcommand{\bhat}{\widehat{\bvec}}\) \(\newcommand{\bperp}{\bvec^\perp}\) \(\newcommand{\xhat}{\widehat{\xvec}}\) \(\newcommand{\vhat}{\widehat{\vvec}}\) \(\newcommand{\uhat}{\widehat{\uvec}}\) \(\newcommand{\what}{\widehat{\wvec}}\) \(\newcommand{\Sighat}{\widehat{\Sigma}}\) \(\newcommand{\lt}{<}\) \(\newcommand{\gt}{>}\) \(\newcommand{\amp}{&}\) \(\definecolor{fillinmathshade}{gray}{0.9}\)- 4.1: Function Calls
- This page explains that a function in programming is a named sequence of statements that performs a computation. It involves defining a name and statements, which can be called later. An example is the function call `type(32)`, where `type` is the function and `32` is its argument. Functions take arguments and return results, with the result being called the return value.
- 4.2: Built-in Functions
- This page discusses Python's built-in functions that facilitate common tasks, such as max, min, and len, which find the largest or smallest values and count items, respectively. For example, max('Hello world') yields 'w' and min returns a space. These functions are applicable to multiple data types and should be treated as reserved words to prevent naming conflicts in programming.
- 4.3: Type Conversion Functions
- This page explains Python's built-in type conversion functions: `int`, `float`, and `str`. The `int` function converts values to integers, discarding fractions and raising errors for non-convertible strings. The `float` function converts integers and strings to floating-point numbers. The `str` function changes its arguments into string format.
- 4.4: Random Numbers
- This page discusses the distinction between deterministic and nondeterministic computer programs, emphasizing the need for unpredictability in certain applications like games. It highlights the challenges of achieving true nondeterminism and introduces pseudorandom number generators as a solution. The Python random module is mentioned, showcasing functions such as `random`, `randint`, and `choice`, which facilitate the generation of random numbers and selection from sequences.
- 4.5: Math Functions
- This page describes the Python math module, which provides a range of mathematical functions accessible via import and dot notation. It includes functionalities for calculating logarithms, trigonometric values, and features constants like π (accurate to 15 digits). Additionally, it explains how to convert degrees to radians and provides examples, such as computing the logarithm of a signal-to-noise ratio and the sine of an angle in radians.
- 4.6: Adding New Functions
- This page explains defining and using functions in Python using the "def" keyword, detailing naming rules and the structure of definitions. It illustrates function creation and invocation with the "print_lyrics" example, discusses function arguments, and emphasizes proper indentation. The text also covers using functions within other functions, exemplified by "repeat_lyrics."
- 4.7: Definitions and Uses
- This page explains a Python program with two functions, `print_lyrics` and `repeat_lyrics`, highlighting the need to define functions before calling them. It includes exercises to rearrange function calls and definitions to observe error messages, enhancing understanding of execution flow and the importance of function definitions in programming.
- 4.8: Flow of Execution
- This page explains that program execution follows a sequential order, starting from the first statement. While function definitions do not alter this order, function calls cause a temporary diversion to execute the called function's statements, after which execution resumes at the original point. Nested function calls can occur, and Python effectively manages this process. Understanding the flow of execution is essential for interpreting and understanding how a program operates.
- 4.9: Parameters and Arguments
- This page explains the use of arguments and parameters in programming functions, differentiating between built-in and user-defined functions. It illustrates a user-defined function, `print_twice`, which prints its argument twice and can take various data types. The page highlights that the argument is evaluated before the function call and notes that parameter names within the function do not conflict with variables in the caller's context.
- 4.10: Fruitful functions and void functions
- This page distinguishes between fruitful functions that return values and void functions that perform actions without returning anything. It emphasizes the necessity of storing results from fruitful functions in variables for usefulness, while void functions yield `None` when assigned. An example function, `addtwo`, demonstrates how to return a value using the return statement.
- 4.11: Why functions?
- This page emphasizes that dividing a program into functions improves readability, simplifies debugging, and minimizes code redundancy. Functions facilitate modifications, as changes are centralized, and allow for step-by-step debugging of complex programs. They also promote code reuse across projects. An example is provided, demonstrating how a function can effectively find the smallest value in a list.
- 4.12: Debugging
- This page advises using spaces for indentation in scripts to prevent debugging issues and suggests choosing a compatible text editor. It emphasizes the importance of saving the program before execution to avoid running outdated code and recommends using print statements for confirmation if confusion arises about which version of the code is running, as debugging will be ineffective otherwise.
- 4.E: Functions (Exercises)
- This page covers Python exercises centered on functions, including identifying the "def" keyword for function definition, predicting outputs of functions "fred" and "jane," rewriting a pay computation to include overtime with "computepay," and transforming a grade calculation into "computegrade" with error handling for invalid scores.
- 4.G: Functions (Glossary)
- This page defines programming concepts like algorithms and functions, explaining terms such as "argument," "body," and "composition." It covers how functions operate with parameters and return values, including "function definitions," "function calls," and "modules." It also discusses "pseudorandom" sequences and differentiates between fruitful functions that return values and void functions that do not.