-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpythonfunctiondefinition.txt
59 lines (43 loc) · 1.23 KB
/
pythonfunctiondefinition.txt
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
49
50
51
52
53
54
55
56
57
58
59
##FUNCTIONS
def sumProblem(x, y):
sum = x + y
sentence = 'The sum of {} and {} is {}.'.format(x, y, sum)
print(sentence)
def main():
sumProblem(2, 3)
sumProblem(1234567890123, 535790269358)
a = int(input("Enter an integer: "))
b = int(input("Enter another integer: "))
sumProblem(a, b)
main()
#STRING FUNCTION
def lastFirst(firstName, lastName):
separator = ', '
result = lastName + separator + firstName
return result
print(lastFirst('Benjamin', 'Franklin'))
print(lastFirst('Andrew', 'Harrington'))
#SCOPE _ LOCAL
def main():
x = 3
f()
def f():
print(x) # error: f does not know about the x defined in main
main()
#CORRECTING BAD SCOPE
def main():
x = 3
f(x)
def f(x):
print(x)
main()
#GLOBAL CONSTANTS
PI = 3.14159265358979 # global constant -- only place the value of PI is set
def circleArea(radius):
return PI*radius*radius # use value of global constant PI
def circleCircumference(radius):
return 2*PI*radius # use value of global constant PI
def main():
print('circle area with radius 5:', circleArea(5))
print('circumference with radius 5:', circleCircumference(5))
main()