+ 1

Def function in python

Can anyone please explain me what actually (def) and (return ) function do?

25th Mar 2024, 9:12 AM
Vivek Maurya
Vivek Maurya - avatar
7 Respostas
+ 6
Lothar, it is strange that the links crashes the apps... Vivek Maurya, as Lothar already said, "def" and "return" are keywords for python. "def" is used to define a function, and "return" is used to return a value which is produced by the function. While a function can accept zero input, most of the time it accepts inputs and produce output. Take an analogy, a water boiler is a function. Fresh water is the input and hot water is the output. Some mechanic inside the boiler is the code inside a function, and you have a cup to hold the hot water. So a "boil_water" function will look something like this: def boil_water(fresh_water): hot_water = heat the fresh_water return hot_water cup_of_water = boil_water(some_water) And here is another example which return a value by increasing 10 from the input. def add10(num): result = num + 10 return result my_num = add10(5) # 15
25th Mar 2024, 11:20 AM
Wong Hei Ming
Wong Hei Ming - avatar
+ 8
Vivek Maurya , your post seems to be incomplete since it lacks of some basic information. (programming language, your code attempt, ...) it looks like you are searching (in python) for more details about *creating functions* and their usage, as well as the using of the *return* statements. (def and return are not functions but statements / keywords) these topics are part of the python tutorials. you may re-reading these topics and do some exercises with it. if you still get stuck, post specific questions here. here are some additional learning sources from the community section about functions in python: https://www.sololearn.com/learn/o-Python/2285/?ref=app https://www.sololearn.com/learn/o-Python/2287/?ref=app
25th Mar 2024, 10:16 AM
Lothar
Lothar - avatar
+ 3
Wong Hei Ming , the 2 links i provided are working properly for me on android app. what issue do you get exactly?
25th Mar 2024, 8:09 PM
Lothar
Lothar - avatar
+ 2
Wong Hei Ming , there is no problem on my galaxy tab s6 lite. the current android version is 13, so may be you can update your device ...
26th Mar 2024, 7:04 AM
Lothar
Lothar - avatar
+ 1
Lothar It displays the page for less than a second, then it crashes and saying "SoloLearn keeps stopping." It happens on my Nokia 7.2 (Android 11) and Amazon Fire HD 10 (Fire OS 7.3.2.9) If I make a search from the community tab, it is not always keep stopping. It is like some pages crash and some don't. Can't figure it out...
26th Mar 2024, 2:59 AM
Wong Hei Ming
Wong Hei Ming - avatar
+ 1
Lothar Nokia 7.2 was released at end of 2019, and it became EOL by end of 2022, so I don't receive update anymore. Unless I unlock and fresh it with custom rom, which I don't feel comfortable to do with my daily working horse.
26th Mar 2024, 7:48 AM
Wong Hei Ming
Wong Hei Ming - avatar
0
Vivek Maurya , Learn these terms (Sololearn doesn't teach them): compound statement clause header suite "Function and class definitions are also syntactically compound statements." -- https://docs.python.org/3/reference/compound_stmts.html#compound-statements
25th Mar 2024, 5:54 PM
Rain
Rain - avatar