-
Notifications
You must be signed in to change notification settings - Fork 1
/
function.py
93 lines (61 loc) · 1.03 KB
/
function.py
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#!/usr/bin/python
# function
def my_function():
print("Hello world!")
# call function
def call_function():
print("Hello world!")
call_function()
# output:
# Hello world!
# parameters
def pa_function(param):
print("Hello", param)
pa_function("Emily")
pa_function("John")
# output:
# Hello Emily
# Hello John
# default parameters
def de_function(name = "Cat"):
print("Hello" + name)
de_function()
de_function("Dog")
# output:
# Hello Cat
# Hello Dog
# return value
def ret_function(x):
return x * 2
print(ret_function(5))
print(ret_function(6))
print(ret_function(2))
# output:
# 10
# 12
# 4
# recursion
def rec_function(x):
if x > 0:
result = x + rec_function(x - 1)
print(result)
else:
result = 0
return result
rec_function(5)
# output:
# 1
# 3
# 6
# 10
# 15
# passing a list as a parameter
numbers = ["one", "two", "three"]
def pas_function(x):
for x in numbers:
print("This is number", x)
pas_function(numbers)
# output:
# This is number one
# This is number two
# This is number three