-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPython Function
More file actions
23 lines (13 loc) · 1.13 KB
/
Python Function
File metadata and controls
23 lines (13 loc) · 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
How to create a function in Python
Create a file called functions.py and type:
def greet(name):
The "def" defines the function that follows it. "Greet" is the name of the function. And then "name" is the argument or an input we place inside the function. Then we "return" a function that will print "Hey {name}!"
def greet(name):
return f"Hey {name}!"
Name isn't defined yet, but that's okay. We'll get to it when its called. And we can call it by typing print(greet('Mattan')). Or any other name you want. The whole thing looks like this:
def age_in_dog_years(age):
result = age * 7
print(age_in_dog_years(28))
Return is how you get back the result of whatever function your running. If you ever get a return of "None" back, it's probably because you forgot to return something.
It's also important to note that each function can only return one thing. If you want to return more than one thing, then you have to return a result as a list or a dictionary. You can also call functions inside of functions.
Remember: variables and functions look the same. You know something is a variable if it doesn't have (). All functions have ().