-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path21functions.py
More file actions
44 lines (27 loc) · 926 Bytes
/
Copy path21functions.py
File metadata and controls
44 lines (27 loc) · 926 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# Functions are a way to reuse code
def func(x, y):
print("Run", x, y)
return x * y, x + y # returns a tuple
print("Calling a function that returns a tuple")
print(func(2, 3)) # (6, 5)
print("========================================")
# Unpacking a tuple
print("Unpacking a tuple")
a, b = func(2, 3)
print(a) # 6
print(b) # 5
print("========================================")
# We can also use default values for parameters
def func(x=1, y=2):
print("Run", x, y)
return x * y, x + y # returns a tuple
print("Calling a function with default values for parameters")
func(2) # Run 2 2
print("========================================")
# My addition - TYPES
# Adding types to parameters and return values
def func(x: int, y: int) -> tuple:
print("Run", x, y)
return x * y, x + y # returns a tuple
print("Calling a function with default values for parameters")
func(2, 3) # Run 2 3