Functions | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Functions

Hi Guys, Quick question. what is the difference between a function with and without a parameter? I Made this code here that keeps asking the user to input a city name. when the user inputs an empty string, it returns the cities in a list. But i didnt use a parameter in the function, but it still worked, not so sure why. def cities(): lst=[] while True: city = input(‘Enter city’) if city == ‘ ‘: return lst lst.append(city) print(cities())

23rd Dec 2019, 12:24 PM
Abdelhakim Dahaman
Abdelhakim Dahaman - avatar
1 Answer
+ 4
If you define a parameter like def f(x): ... you MUST call the function with an argument. If you define it without, like def f(): ... you MUST NOT give an argument when you call it. However, if you give your parameter a default value, like def f(x=1): ... Then you CAN give an argument, but don't need to - if you don't, then the argument will be 1.
23rd Dec 2019, 1:29 PM
HonFu
HonFu - avatar