-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path011-functions.py
More file actions
48 lines (32 loc) · 1.15 KB
/
011-functions.py
File metadata and controls
48 lines (32 loc) · 1.15 KB
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
45
46
47
48
'''
Functions
'''
# [BASIC FUNCTION]: with 2 parameters
def addition(x,y):
return x + y
# Print the call to the function
print(addition(1,2)) # Output: 3
# [FUNCTION WITH A DEFAULT PARAMETER & 2 MULTIPLE RETURN VALUES]
def sumAndProduct(x, y=9):
return x + y, x * y
print(sumAndProduct(2, 3)) # Output: (5, 6)
print(sumAndProduct(2)) # Output: (11, 18)
# Save the multiple returned values in variables
addition, product = sumAndProduct(5, 6)
print(addition) # Output: 11
print(product) # Output: 30
# [KEYWORD ARGUMENTS]
def colors(a, b):
print("a:", a, "b:", b)
return "KEEP GOING"
colors(1, 2) # Output: a: 1 b: 2
colors(2, 1) # Output: a: 2 b: 1
# Keyword arguments: arguments are passed using their names
colors(b=2, a=1) # Output: a: 1 b: 2
# [SCOPE: GLOBAL VS LOCAL]
testVar = "global" # Global variable
def checkScope():
testVar = "local" # Local variable, can't be used outside function
print(testVar)
print(testVar) # Output: global
checkScope() # Output: local