Skip to content

Commit 51062c6

Browse files
start functions and classes notebook
1 parent ccd69bf commit 51062c6

File tree

1 file changed

+61
-5
lines changed

1 file changed

+61
-5
lines changed

Functions_and_Classes.ipynb

Lines changed: 61 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,72 @@
11
{
22
"cells": [
33
{
4-
"cell_type": "code",
5-
"execution_count": null,
6-
"id": "initial_id",
4+
"metadata": {},
5+
"cell_type": "markdown",
6+
"source": [
7+
"# functions and classes\n",
8+
"\n",
9+
"soooo i havent exactly been being a great writer and explaining alll the code that ive been writing. you've been seeing a lot of code snippets that are using functions and i havent exactly explained what those are yet! i've gone into them in some smaller detail in one of the lessons, but today we're going to do a deeper dive (or maybe you could say delve ahaha) into functions and classes!\n",
10+
"\n",
11+
"So, functions are a block of code that can be reused. It's good for modularization and compartmentalization of your code, especially if you have a lot of code that does the same thing. You can write it once, and then call it multiple times. This is useful for things like calculations, data processing, or any repetitive task.\n",
12+
"\n",
13+
"each function can take in parameters, which are inputs that the function uses to perform its task. It can also return a value, which is the output of the function. This allows you to pass data into the function and get results back. you declare a function using the `def` keyword, followed by the function name and parentheses containing any parameters. The body of the function is indented below the definition. when the indentation goes back to its previous level, that means the definition of the function is over.\n",
14+
"take a look at a few examples below :"
15+
],
16+
"id": "718e27f5bfe0fec6"
17+
},
18+
{
719
"metadata": {
820
"collapsed": true
921
},
22+
"cell_type": "code",
23+
"outputs": [],
24+
"execution_count": null,
25+
"source": [
26+
"# the most basic function, some code under a header\n",
27+
"def print_hello():\n",
28+
" print(\"Hello, world!\")\n",
29+
"\n",
30+
"# take a paramater (input) into the function\n",
31+
"def greet(name):\n",
32+
" print(f\"Why hello there {name}!\")\n",
33+
"\n",
34+
"# a function that takes in two parameters and returns a value\n",
35+
"def exponentiate(a,b):\n",
36+
" return a ** b\n",
37+
"\n",
38+
"print_hello()\n",
39+
"greet() # put your name here!!\n",
40+
"exponentiate(9,3)"
41+
],
42+
"id": "initial_id"
43+
},
44+
{
45+
"metadata": {},
46+
"cell_type": "markdown",
47+
"source": [
48+
"## recursive functions\n",
49+
"\n",
50+
"a recursive function is a function that calls itself. this is useful for problems that can be broken down into smaller subproblems, like calculating factorials or traversing trees. the key to a recursive function is to have a base case that stops the recursion, otherwise it will run indefinitely and cause a stack overflow error.\n",
51+
"take a look at the example below, which calculates a factorial, which is the product of each number smaller than itself to 1. for example, 5! = 5 * 4 * 3 * 2 * 1 = 120. the base case is when n is 0 or 1, in which case the factorial is 1. otherwise, the function calls itself with n - 1 until it reaches the base case."
52+
],
53+
"id": "2f14750a7071b0c"
54+
},
55+
{
56+
"metadata": {},
57+
"cell_type": "code",
1058
"outputs": [],
59+
"execution_count": null,
1160
"source": [
12-
""
13-
]
61+
"def factorial (n):\n",
62+
" if n == 0 or n == 1: # base case\n",
63+
" return 1\n",
64+
" else:\n",
65+
" return n * factorial(n - 1) # recursive case\n",
66+
"\n",
67+
"print(factorial(6))"
68+
],
69+
"id": "91a29a6bd518edd3"
1470
}
1571
],
1672
"metadata": {

0 commit comments

Comments
 (0)