|
| 1 | +# Defining your own functions |
| 2 | + |
| 3 | +It's probably been a while since you read about using functions. [Read |
| 4 | +about it again](using-functions.md) if you need to. |
| 5 | + |
| 6 | +## Why should I use custom functions? |
| 7 | + |
| 8 | +Have a look at this code: |
| 9 | + |
| 10 | +```py |
| 11 | +print("************") |
| 12 | +print("Hello World!") |
| 13 | +print("************") |
| 14 | +print() |
| 15 | + |
| 16 | +print("********************") |
| 17 | +print("Enter your password:") |
| 18 | +print("********************") |
| 19 | +print() |
| 20 | + |
| 21 | +word = input() |
| 22 | + |
| 23 | +if word == 'python': |
| 24 | + print("********") |
| 25 | + print("Welcome!") |
| 26 | + print("********") |
| 27 | + print() |
| 28 | +else: |
| 29 | + print("**************") |
| 30 | + print("Access denied.") |
| 31 | + print("**************") |
| 32 | + print() |
| 33 | +``` |
| 34 | + |
| 35 | +Then compare it to this code: |
| 36 | + |
| 37 | +```py |
| 38 | +print_box("Hello World!") |
| 39 | +print_box("Enter your password:") |
| 40 | +word = input() |
| 41 | +if word == 'python': |
| 42 | + print_box("Welcome!") |
| 43 | +else: |
| 44 | + print_box("Access denied.") |
| 45 | +``` |
| 46 | + |
| 47 | +That's nice, but where do we a box function like that? |
| 48 | + |
| 49 | +## First functions |
| 50 | + |
| 51 | +The `pass` keyword does nothing. |
| 52 | + |
| 53 | +```py |
| 54 | +>>> pass |
| 55 | +>>> |
| 56 | +``` |
| 57 | + |
| 58 | +Let's use it to define a function that does nothing. |
| 59 | + |
| 60 | +```py |
| 61 | +>>> def do_nothing(): |
| 62 | +... pass |
| 63 | +... |
| 64 | +>>> do_nothing |
| 65 | +<function do_nothing at 0x7f56b74e9598> |
| 66 | +>>> |
| 67 | +``` |
| 68 | + |
| 69 | +Seems to be working so far, we have a function. Let's see what happens |
| 70 | +if we call it. |
| 71 | + |
| 72 | +```py |
| 73 | +>>> do_nothing() |
| 74 | +>>> |
| 75 | +``` |
| 76 | + |
| 77 | +There we go. It did nothing at all. |
| 78 | + |
| 79 | +Maybe we could just do something in the function instead? |
| 80 | + |
| 81 | +```py |
| 82 | +>>> def print_hi(): |
| 83 | +... print("Hi!") |
| 84 | +... |
| 85 | +>>> print_hi() |
| 86 | +Hi! |
| 87 | +>>> |
| 88 | +``` |
| 89 | + |
| 90 | +It's working. How about printing a variable in the function? |
| 91 | + |
| 92 | +```py |
| 93 | +>>> message = "Hello World!" |
| 94 | +>>> print_message() |
| 95 | +Hello World! |
| 96 | +>>> |
| 97 | +``` |
| 98 | + |
| 99 | +Again, it works. How about setting a variable in the function? |
| 100 | + |
| 101 | +```py |
| 102 | +>>> def set_username(): |
| 103 | +... username = 'me' |
| 104 | +... |
| 105 | +>>> set_username() |
| 106 | +>>> username |
| 107 | +Traceback (most recent call last): |
| 108 | + File "<stdin>", line 1, in <module> |
| 109 | +NameError: name 'username' is not defined |
| 110 | +>>> |
| 111 | +``` |
| 112 | + |
| 113 | +That was weird! Why didn't that work? |
| 114 | + |
| 115 | +## Locals and globals |
| 116 | + |
| 117 | +So far we have used nothing but **global variables**. They are called |
| 118 | +globals because the same variables are available anywhere in our |
| 119 | +program, even in functions. |
| 120 | + |
| 121 | +```py |
| 122 | +>>> a = 1 |
| 123 | +>>> b = "hi" |
| 124 | +>>> c = "hello there" |
| 125 | +>>> def print_abc(): |
| 126 | +... print(a, b, c) |
| 127 | +... |
| 128 | +>>> print_abc() |
| 129 | +1 hi hello there |
| 130 | +>>> |
| 131 | +``` |
| 132 | + |
0 commit comments