class: big, middle # Engineering 1020: Introduction to Programming .title[ .lecture[Lecture 2:] .title[Functions] ] .footer[[/lecture/2/](/lecture/2/)] --- # Last time: ## Expressions -- _Values_ and _operations_ that _evaluate_ to a _value_ --- # Values ### Literals: * integer literals (e.g., `42`) * _floating-point_ (real-valued) literals (e.g., `3.14`) * _string_ literals (e.g., `'hello'`) * _Boolean_ (logical) literals (e.g., `True`) ### Variables --- # Variables ### Named values _Actually, it's slightly more complicated than that, but..._ ```python >>> from math import * >>> pi 3.141592653589793 >>> 2j * pi 6.283185307179586j ``` ```python r1 + r2 ``` ??? We'll talk more about variables in later lectures when we talk about how to **create** them. For now we will just focus on **using** them. --- # Operations ### Arithmetic operators * addition, substraction, multiplication, division (both kinds) * exponentiation --- # Today: more operators! .floatleft[ ### Values * literals ✅ * variables ✅ ] .floatleft[ ### Operators * arithmetic ✅ * function calls * logical operators (**Wednesday**) ] --- # Function calls There's lots we _will_ say about functions, but for now... ??? A function is a really important idea in programming. Throughout this course, we will be using and creating functions; creating them is where a lot of the **complexity** comes in. For now, we will start by looking at how we **use** functions. -- ### What are mathematical functions? -- e.g., $\sin(x)$ -- ### Calling Python functions: ```python >>> from math import * >>> sin(pi) 1.2246467991473532e-16 ``` ??? Again, this is **close** to 0 but not **exactly** so! --- # Machine model .center[ <img src="../images/functions/machine-model/cos.png" height="140"/> ] ??? In programming, we should instead think of a function as a machine for producing output when given input. -- .center[ <img src="../images/functions/machine-model/cos-1.png" height="140"/> ] -- .center[ <img src="../images/functions/machine-model/cos-half.png" height="140"/> ] --- # Multiple inputs ### Can have more than one input: .floatleft[ <img src="../images/functions/machine-model/f-12.png" height="200"/> ] -- ### Corresponding Python code: ```python Python 3.9.1 >>> f(1, 2) 1 ``` -- ### We'll talk about how to _make_ functions later ??? For now, this will let us **use** functions. --- # Some functions Some functions that we can use right now: .floatleft[ #### General | | |-| | `help` | `print` ] -- .floatleft[ #### Trigonometry* | | |-| | `sin` | `asin` | `cos` | `acos` | `tan` | `atan` ] .footnote[ * Type `from math import *` before using mathematical functions ] -- .floatleft[ #### Other math* | | |-| | `ceil` | `floor` | `degrees` | `radians` | `factorial` | `gcd` | `lcm` ] ??? Using these simple functions, you should now be able to write Python expressions to address math and science problems in your other Engineering One courses. So, when calculating something by hand, why not _also_ try writing the answer as a Python expression to help practice programming too? Then you can put your other course work to work for you in multiple ways! --- # More operator precedence .floatleft[ | Operator | Description | |---------------|------------------| | `()` | Parentheses | `x(args...)` | Function call | `**` | Expon. | `+x`, `-x`, `~x` | Unary | `*`, `/`, `//`, `%` | Mult. | `+`, `-` | Add. ] -- ## ... and more to come! --- class: big, middle (here endeth the lesson)