# Functions
A function is a block of statements that is written for a specific task. Using functions allows us to break down our program into small and modular pieces.
We can pass data into a function. A function can return data as well.
# Create a function
We use def
to define a function.
def say_hello():
print("hello")
2
We use return
to specify the return value.
def greeting(name):
return "hello " + name
2
# Call a function
We call a function by its name followed by parenthesis.
say_hello()
print(greeting("Jack"))
2
3
# Arguments
Arguments are defined inside the parenthesis when we def
a function. We can specify any number of arguments, separated by commas.
When we call a function, we need to provide the matching number of arguments.
say_hello("Jack")
print(greeting())
print(greeting("Jack", "Rose"))
2
3
4
5
However if we specify default values, we can call the function without providing those arguments.
Just like the get()
function of a dictionary.
def greeting(name="Nobody"):
return "hello " + name
print(greeting())
2
3
4
The order of arguments can be changed if we use keyword arguments.
def smallest_number(value1, value2, value3):
print("value1:", value1)
print("value2:", value2)
print("value3:", value3)
return min([value1, value2, value3])
print(smallest_number(10, 213, 2))
print(smallest_number(value3=10, value1=213, value2=2))
2
3
4
5
6
7
8
9
We can also specify a function to take arbitrary number of arguments.
- Adding
*
before the argument name make it receive a tuple of arguments
def smallest_number2(value1, value2, value3, *other_values):
print("value1:", value1)
print("value2:", value2)
print("value3:", value3)
print("other_values:", other_values)
return min([value1, value2, value3] + list(other_values))
print(smallest_number2(10, 213, 2, 123, 928, -23, -7661))
2
3
4
5
6
7
8
- Adding
**
before the argument name make it receive a dictionary of arguments
def smallest_number3(value1, value2, value3, **other_values):
print("value1:", value1)
print("value2:", value2)
print("value3:", value3)
print("other_values:", other_values)
return min([value1, value2, value3] + list(other_values.values()))
print(smallest_number3(10, 213, 2, value7=123, value4=928, value5=-23, value6=-7661))
2
3
4
5
6
7
8
- Arbitrary positional arguments and keyword arguments can be used together
def smallest_number4(value1, value2, value3,*other_values1, **other_values2):
print("value1:", value1)
print("value2:", value2)
print("value3:", value3)
print("other_values1:", other_values1)
print("other_values2:", other_values2)
return min(
[value1, value2, value3] + list(other_values1) + list(other_values2.values())
)
print(
smallest_number4(
10, 213, 2, 2727, 23, 982, value7=123, value4=928, value5=-23, value6=-7661
)
)
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# lambda function
A lambda function is a small anonymous function. It can take any number of arguments, but can only have one statement.
add_10 = lambda value : value + 10
print(add_10(0))
2
multiply = lambda a, b : a*b
print(multiply(5.5, 9.9))
2
# Assignment 12
Create a Python script named terminal_drawing.py
that integrates the drawing functionalities in Assignment 4, Assignment 5, Assignment 6, and Assignment 7.
It will prompt the user with a list of available options to create shapes in the terminal, like the following.
1: box
2: diamond
3: triangle
4: plus
Enter your selection:
2
3
4
5
If the user enters number 1-4, the corresponding shape will be created, with the help of extra user input to determine the size of that shape. After the shape is created, the program would allow user to make another selection.
If the use enters anything other than number 1-4, the program would say "Goodbye!" and exit.
TIP
Create a function for each shape, so that we can call that specific function to draw the shape upon selection.
You can reuse your solutions for those assignments, with little adjustment to wrap them in the context of a function.
A sample run looks like the following.
python terminal_drawing.py
1: square
2: diamond
3: triangle
4: plus
Enter your selection: 1
Enter number of rows for the square: 5
*****
* *
* *
* *
*****
1: square
2: diamond
3: triangle
4: plus
Enter your selection: 2
Enter number of rows for the diamond: 5
*
***
*****
***
*
1: square
2: diamond
3: triangle
4: plus
Enter your selection: 3
Enter number of rows for the triangle: 5
*
**
***
****
*****
1: square
2: diamond
3: triangle
4: plus
Enter your selection: 4
Enter number of rows for the plus: 5
*
*
*****
*
*
1: square
2: diamond
3: triangle
4: plus
Enter your selection: 6
Goodbye!
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
Sample Solution
def square():
rows = int(input("Enter number of rows for the square: "))
row = 0
while row < rows:
if row == 0 or row == rows - 1:
print("*" * rows)
else:
print("*" + " " * (rows - 2) + "*")
row += 1
def triangle():
rows = int(input("Enter number of rows for the triangle: "))
for i in range(rows):
print(" " * (rows - i - 1) + "*" * (i + 1))
def plus():
while True:
rows = int(input("Enter number of rows for the plus: "))
if rows % 2:
break
i = 0
while i < rows:
if i != rows // 2:
print(" " * (rows // 2) + "*")
else:
print("*" * rows)
i += 1
def diamond():
while True:
rows = int(input("Enter number of rows for the diamond: "))
if rows % 2:
break
for i in range(0, rows // 2):
print(" " * (rows // 2 - i) + "*" * (2 * i + 1))
print("*" * rows)
for i in range(rows // 2 - 1, -1, -1):
print(" " * (rows // 2 - i) + "*" * (2 * i + 1))
mapping = {
"1": square,
"2": diamond,
"3": triangle,
"4": plus,
}
while True:
print("1: square")
print("2: diamond")
print("3: triangle")
print("4: plus")
selection = input("Enter your selection: ")
func = mapping.get(selection)
if not func:
print("Goodbye!")
break
func()
print()
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
← Set Data Type Modules →