1+ #We are going to wrap back around to lists and loops
2+ #but first functions
3+ #functions are the only way to build scalable code
4+
5+
6+ ########## HOW TO CREATE A FUNCTION ##########
7+
8+
9+ #This one is simple, but we go on.
10+ def greet ():
11+ print ("hello" )
12+ print ("Welcome, Caleb" )
13+
14+ #invoke
15+ greet ()
16+ #Can invoke numerous times:
17+ greet ()
18+ greet ()
19+
20+ #Benefits: We can save lines of code if function is large
21+ #One source of truth. No repeat of code. Need changes?
22+ #update one spot
23+ #Can improve code readability
24+
25+
26+ ########## Arguments and Parameters ##########
27+
28+
29+ #parameter is a variable used within a function
30+ def greet (name ): #name is a parameter
31+ print ("hello" )
32+ print ("Welcome, " + name )
33+
34+ greet ("Sabrina" ) #Sabrina is an argument (value passed in to parameter variable)
35+
36+ #We can also use variables as arguments
37+
38+ new_guy = "Carl"
39+ greet (new_guy )
40+
41+ #This gives the value "Carl" to the name parameter
42+
43+
44+ ########## RETURN ##########
45+
46+
47+ #We can return (exit) from a function in certain situations
48+ def greet (name ):
49+ if name == "Claire" :
50+ return
51+ else :
52+ print ("hello" )
53+ print ("Welcome " + name )
54+
55+ greet ("Claire" ) #NOPE!
56+ greet ("Clarice" ) #YES!
57+
58+ #This code can be simplified by removing the else:
59+
60+ def greet (name ):
61+ if name == "Claire" :
62+ return
63+ print ("hello" )
64+ print ("Welcome " + name )
65+
66+ #The concern without an else is that the line after the if is announced regardless of name
67+ #This works, however, because there is never a case where name == "Claire" that is not returned
68+ #We only need else if we are not returning!
69+
70+
71+ ########## RETURN VALUES ##########
72+
73+
74+ #instead of printing inside the function, we can use the function to generate a message
75+ #We can print outside of the function on the caller side
76+ #caller side --> Where we invoke the function
77+
78+ def greet (name ):
79+ if name == "Claire" :
80+ return "Go away"
81+ return "Hello " + name + "! Welcome to my app."
82+
83+ returned = greet ("Sal" )
84+ print (returned )
85+
86+ #We can use the returned value directly...
87+ print (greet ("Claire" ))
88+
89+
90+ ########## DEFAULT ARGUMENTS ##########
91+
92+
93+ #We may want to create a method that does not require an input
94+ def greet (name = "User" ):
95+ if name == "Claire" :
96+ return "Go away"
97+ return "Hello " + name + "! Welcome to my app."
98+
99+ print (greet ()) #No name? Default to User
100+
101+
102+ ########## MULTIPLE ARGUMENTS / PARAMETERS ##########
103+
104+
105+ def greet (name = "User" , be_nice = False ):
106+ if be_nice :
107+ return "Hello " + name + "! Welcome to my app."
108+ return "Go away " + name
109+
110+ print (greet ("Caleb" , True ))
111+ print (greet ("Caleb" ))
112+
113+
114+ ########## KEYWORD ARGUMENTS ###########
115+
116+ #This will showcase how to skip any particular parameter
117+
118+ def greet (name = "User" , be_nice = False ):
119+ if be_nice :
120+ return "Hello " + name + "! Welcome to my app."
121+ return "Go away " + name
122+
123+ print (greet (be_nice = True ))
124+
125+ #NOTE if a parameter does not have a default value, you must specify it
126+ #Either in order without a name, or assigning by name
127+
128+
129+ ########## POSITIONAL OR KEYWORD ARGUMENTS ##########
130+
131+
132+ #With the function we have defined, we can pass arguments by position or by keyword
133+
134+ def greet (name , be_nice ):
135+ if be_nice :
136+ return "Hello " + name + "! Welcome to my app."
137+ return "Go away " + name
138+
139+
140+ print (greet ("Positional" , True )) #Positional
141+ print (greet (be_nice = True , name = "Keyword" )) #Keyword (NOTICE ORDER!)
142+
143+ #Positional arguments must follow keyword arguments
144+ #ERROR:
145+ #print(greet(be_nice=True, "Keyword"))
146+
147+ #NOTE that we can invoke by position or keyword. We can restrict to either though.
148+ #If we want our method to have to be invoked using position, we can do that
149+ #We can also restrict to keyword only as well.
150+
151+
152+ ########## POSITIONAL-ONLY ARGUMENTS ##########
153+
154+
155+ def greet (name , be_nice , / ): #Anything before the / must be positional
156+ if be_nice :
157+ return "Hello " + name + "! Welcome to my app."
158+ return "Go away " + name
159+
160+
161+ print (greet ("positional" , False ))
162+ #ERROR because invoking with keyword:
163+ #print(greet("Positional", be_nice=True))
164+
165+ #We can also put the / between arguments
166+ def greet (name , / , be_nice ):
167+ if be_nice :
168+ return "Hello " + name + "! Welcome to my app."
169+ return "Go away " + name
170+
171+ print (greet ("Positional" , be_nice = True )) #This works now.
172+ print (greet ("Positional" , True )) #This works too.
173+
174+ #We can invoke with be_nice being named or positional
175+ #Doesn't add a lot of value but nice when we have lots of arguments
176+
177+ #ERROR because invoking name by keyword:
178+ #print(greet(name="Keyword", be_nice=True))
179+
180+
181+ ########## KEYWORD-ONLY ARGUMENTS ##########
182+
183+ #Place a *, before any arguments that are keyword-only
184+ def greet (name , / , * , be_nice ): #be_nice is keyword only
185+ if be_nice :
186+ return "Hello " + name + "! Welcome to my app."
187+ return "Go away " + name
188+
189+ print (greet ("Positional" , be_nice = True )) #be_nice= is required
190+
191+ #ERROR because positional
192+ #print(greet("Positional", True))
193+
194+
195+ ########## SPECIAL PARAMETER SUMMARY ##########
196+
197+ #We've learned about positional-only arguments, and keyword-only arguments
198+ #And of course the arguments that can be either.
199+ #Here is full (but contrived) example
200+
201+ def do_nothing (pos1 , pos2 , pos3 , / , either1 , either2 , * , keyword1 , keyword2 , keyword3 ):
202+ print ("pos1" , pos1 )
203+ print ("pos2" , pos2 )
204+ print ("pos3" , pos3 )
205+ print ("either1" , either1 )
206+ print ("either2" , either2 )
207+ print ("keyword1" , keyword1 )
208+ print ("keyword2" , keyword2 )
209+ print ("keyword3" , keyword3 )
210+
211+ do_nothing (1 , 2 , 3 , 4 , either2 = 5 , keyword3 = 6 , keyword1 = 7 , keyword2 = 8 )
212+
213+ #A very helpful rule to remember the positioning of this junk
214+ #You must always invoke a method passing positional first
215+ #Because if you passed them later, the position would be janked up
216+ #The either parameters must be in the middle because they can also be positional
217+ #The keyword are always last because you can mix their order
218+
219+
220+ ########## LIST PARAMETERS / FUNCTION TO ITERATE LIST #########
221+
222+
223+ def greet_all (people ):
224+ for person in people :
225+ print ("Hello " + person )
226+
227+ people = ["Caleb" , "Josh" , "Austin" ]
228+
229+ greet_all (people )
230+
231+
232+ ########## UNLIMITTED ARGUMENTS / PACKING ##########
233+
234+
235+ #What if you are working with individual pieces of data?
236+ #We can modify the function to "pack" the data with *
237+
238+ def greet_all (* people ):
239+ for person in people :
240+ print ("Hello " + person )
241+
242+
243+ greet_all ("Caleb" , "Josh" , "Austin" )
244+
245+
246+ ########## UNPACKING ##########
247+
248+
249+ def print_info (name , age , email ):
250+ print (name + " is " + str (age ) + ". Reached at " + email )
251+
252+ info = [
"Caleb" ,
12 ,
"[email protected] " ]
253+ print_info (* info )
254+
255+
256+ ########## FUNCTIONS CALLING FUNCTIONS ##########
257+
258+ #Let's say we have this method and no way to edit it
259+ def greet (name , be_nice = True ):
260+ if be_nice :
261+ return "Hello " + name + "! Welcome to my app."
262+ return "Go away " + name
263+
264+ #We can utilize it.
265+ def greet_all (people ):
266+ for person in people :
267+ print (greet (person )) // because it returns string
268+
269+ people_to_greet = ["Johnny" , "Jimmy" , "Jacob" ]
270+ greet_all (people_to_greet )
0 commit comments