-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathFunction_parameters.py
123 lines (96 loc) · 4.16 KB
/
Function_parameters.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# We already learned to create functions which accept a parameter and return values
def get_initial(name):
initial = name[0:1].upper()
return initial
first_name = input('Enter your first name: ')
first_name_initial = get_initial(first_name)
print('Your initial is: ' + first_name_initial)
# Functions can accept multiple parameters
def get_initial(name, force_uppercase):
if force_uppercase:
initial = name[0:1].upper()
else:
initial = name[0:1]
return initial
first_name = input('Enter your first name: ')
first_name_initial = get_initial(first_name, False) # Pass the parameters in the same order they are listed in the function declaration
print('Your initial is: ' + first_name_initial)
# You can specify a default value for a parameter
def get_initial(name, force_uppercase=True):
if force_uppercase:
initial = name[0:1].upper()
else:
initial = name[0:1]
return initial
first_name = input('Enter your first name: ')
first_name_initial = get_initial(first_name)
print('Your initial is: ' + first_name_initial)
# You can also assign the values to parameters by name when you call the function
def get_initial(name, force_uppercase):
if force_uppercase:
initial = name[0:1].upper()
else:
initial = name[0:1]
return initial
first_name = input('Enter your first name: ')
first_name_initial = get_initial(force_uppercase=True, name= first_name) # When you use named parameters, you can specify parameters in any order
print('Your initial is: ' + first_name_initial)
# Using the named notation when calling functions makes your code more readable
def error_logger(error_code, error_severity, log_to_db,\
error_message, source_module):
print('oh no error ' + error_message)
# Imagine code here that logs our error to a database or file
first_number = 10
second_number = 5
if first_number > second_number:
error_logger(45, 1, True,
'Second number greater than first',
'my_math_method')
# We already learned to create functions which accept a parameter and return values
def get_initial(name):
initial = name[0:1].upper()
return initial
first_name = input('Enter your first name: ')
first_name_initial = get_initial(first_name)
print('Your initial is: ' + first_name_initial)
# Functions can accept multiple parameters
def get_initial(name, force_uppercase):
if force_uppercase:
initial = name[0:1].upper()
else:
initial = name[0:1]
return initial
first_name = input('Enter your first name: ')
first_name_initial = get_initial(first_name, False) # Pass the parameters in the same order they are listed in the function declaration
print('Your initial is: ' + first_name_initial)
# You can specify a default value for a parameter
def get_initial(name, force_uppercase=True):
if force_uppercase:
initial = name[0:1].upper()
else:
initial = name[0:1]
return initial
first_name = input('Enter your first name: ')
first_name_initial = get_initial(first_name)
print('Your initial is: ' + first_name_initial)
# You can also assign the values to parameters by name when you call the function
def get_initial(name, force_uppercase):
if force_uppercase:
initial = name[0:1].upper()
else:
initial = name[0:1]
return initial
first_name = input('Enter your first name: ')
first_name_initial = get_initial(force_uppercase=True, name= first_name) # When you use named parameters, you can specify parameters in any order
print('Your initial is: ' + first_name_initial)
# Using the named notation when calling functions makes your code more readable
def error_logger(error_code, error_severity, log_to_db,\
error_message, source_module):
print('oh no error ' + error_message)
# Imagine code here that logs our error to a database or file
first_number = 10
second_number = 5
if first_number > second_number:
error_logger(45, 1, True, # (error_code=45, error_severity=1, log_to_db=True,
'Second number greater than first', # error_message='Second number greater than first',
'my_math_method') # source_module='my_math_method')