# Languages & Functions Week 7 --- ## Review  <!-- .element: style="height:400px" --> -- What does CPU stand for? Central Processing Unit <!-- .element: class="fragment green" --> -- What does a CPU do? Lots of math (computer brain) <!-- .element: class="fragment green" --> -- What are the three _logical operators_ in Python? (aka boolean operators) `and`, `or`, `not` <!-- .element: class="fragment green" --> -- What is the output? ```py print(not True) ``` ``` False ``` <!-- .element: class="fragment green" --> -- What is the output? ```py print(True and True or False) ``` ``` True ``` <!-- .element: class="fragment green" --> --- ## Programming Languages <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" style="width:100px;fill:currentColor"><path d="M9,4H15V12H19.84L12,19.84L4.16,12H9V4Z" /></svg> -- What is a language? System of words to communicate <!-- .element: class="fragment green" --> -- Levels of Abstraction - Machine code <!-- .element: class="fragment" --> - Assembly language <!-- .element: class="fragment" --> - Low-level <!-- .element: class="fragment" --> - High-level <!-- .element: class="fragment" --> NOTES: - Binary - x86, ARM, MIPS - C, Rust - Python, JavaScript, C#, Swift -- Machine Code (binary) ``` 0110 1000 0010 0001 0000 1010 0000 0000 1010 0011 0000 1101 0000 0000 0000 0110 0110 1000 0110 1111 0111 0010 0110 1100 0110 0100 1010 0011 0000 1000 0000 0000 0000 0110 0110 1000 0110 1111 0010 1100 0010 1000 0101 0111 1010 0011 0000 0100 0000 0000 0000 0110 0110 1000 0100 1000 0110 0101 0110 1100 0110 1100 1010 0011 0000 0000 0000 0000 0000 0110 1011 0000 0000 0000 0000 0000 0000 0110 1011 0000 0000 0000 0000 0000 0000 0000 1011 0000 0000 0000 0000 0000 0000 0001 0110 1000 0000 0100 0000 0000 0000 0000 1100 1101 1000 0000 0110 1000 0000 0001 0000 0000 0000 0000 1100 1101 1000 0000 ``` -- Assembly (RISC) ```asm .data hello: .asciiz "Hello, World!\n" .text main: li a0, 4 la a0, hello li a7, 4 ecall li a7, 10 ecall ``` -- Low-Level Language (C) ```c int main() { printf("Hello, World!\n"); return 0; } ``` -- High-Level Language (Python) ```py print("Hello, World!") ``` --- ## Common Languages <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" style="width:100px;fill:currentColor"><path d="M9,4H15V12H19.84L12,19.84L4.16,12H9V4Z" /></svg> -- C++ ```cpp #include <iostream> #include <string> int main() { std::string user_input; std::cout << "Enter something: "; std::getline(std::cin, user_input); std::cout << user_input << std::endl; return 0; } ``` -- Java ```java import java.util.Scanner; public class Echo { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter something: "); String userInput = scanner.nextLine(); System.out.println(userInput); } } ``` -- JavaScript ```js const readline = require('readline').createInterface({ input: process.stdin, output: process.stdout }); readline.question('Enter something: ', (userInput) => { console.log(userInput); readline.close(); }); ``` -- Python ```py user_input = input("Enter something: ") print(user_input) ``` --- ## Semantics vs. Syntax <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" style="width:100px;fill:currentColor"><path d="M9,4H15V12H19.84L12,19.84L4.16,12H9V4Z" /></svg> -- What are "semantics?" The meaning we convey with language <!-- .element: class="fragment green" --> What is "syntax?" Words, character, grammar <!-- .element: class="fragment green" --> -- Comments are for _semantics_ ```py [] # TODO Tell the user "hello" ``` -- More than one valid syntax ```py [2|3|4] # Tell the user "hello" print("Hello") print( f'Hello' ) print('H' + 'e' + 'l' + 'l' + 'o') ``` (some are better than others) <!-- .element: class="fragment red" --> -- Syntactic sugar ```py [|3-4|6-7] if levi_has_fingers: print("High five!") else: print("RIP Levi's digits") if not levi_has_fingers: print("RIP Levi's digits") ``` --- ## Functions <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" style="width:100px;fill:currentColor"><path d="M9,4H15V12H19.84L12,19.84L4.16,12H9V4Z" /></svg> -- What is a function? A reusable section of code <!-- .element: class="fragment green" --> -- Define + call ```py [1-3|5-6] # Function definition def print_foo(): print("foo") # Function call print_foo() ``` -- Definition ```py [3-5] message = "" def laugh_contagiously(): message += "ha" print(message) laugh_contagiously() laugh_contagiously() laugh_contagiously() ``` -- Call ```py [7-9] message = "" def laugh_contagiously(): message += "ha" print(message) laugh_contagiously() laugh_contagiously() laugh_contagiously() ``` --- ## Parameters <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" style="width:100px;fill:currentColor"><path d="M9,4H15V12H19.84L12,19.84L4.16,12H9V4Z" /></svg> -- f(x) = -x <iframe src="https://www.desmos.com/calculator/z1utayfoo1?embed" width="600" height="400" frameborder=0></iframe> -- f(x) = -x | x | y | |-----|--------| | 5 | <span class="fragment green">-5</span> | | 0 | <span class="fragment green">0</span> | | -10 | <span class="fragment green">10</span> | -- f(x) = -x ```py [] def f(x): print(x * -1) ``` -- ```py [] def say_hello(name): print(f"Hello, {name}!") say_hello("Tim") say_hello("Skyler") say_hello("Nolan") say_hello("Levi") ``` -- ```py [] def print_sum(x, y): print(x + y) print_sum(4, 6) print_sum(-1000, 1) ``` ```txt 10 -999 ``` <!-- .element: class="fragment" --> --- ## Return <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" style="width:100px;fill:currentColor"><path d="M9,4H15V12H19.84L12,19.84L4.16,12H9V4Z" /></svg> -- f(x) = -x ```py [|2|4|5] def f(x): return x * -1 y = f(5) print("f(5) is", y) ``` -- What does this output? ```py [] def compute_sum(x, y): return x + y compute_sum(4, 6) compute_sum(-1000, 1) ``` Nothing! <!-- .element: class="fragment red" --> -- ```py [] def compute_sum(x, y): return x + y print( compute_sum(4, 6) ) print( compute_sum(-1000, 1) ) ``` ```txt 10 -999 ``` <!-- .element: class="fragment" --> -- What does this output? ```py [] def say_hi(name): print( "Hi", name ) def append_hi(name): return "Hi " + name print( say_hi("Steve") ) print( append_hi("Steve") ) ``` ```txt Hi Steve None Hi Steve ``` <!-- .element: class="fragment" --> --- ## Exercise <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" style="width:100px;fill:currentColor"><path d="M9,4H15V12H19.84L12,19.84L4.16,12H9V4Z" /></svg> -- Implement functions so this ad-lib program works! ```py noun1 = get_word("noun") noun2 = get_word("noun") verb = get_word("verb") print_story(noun1, noun2, verb) ``` ---  <!-- .element: style="height:400px" -->