def() | Sololearn: Learn to code for FREE!
¡Nuevo curso! ¡Todo programador debería aprender IA Generativa!
Prueba una lección gratuita
0

def()

Guys, help me plz with function def(). I can’t understand this. Thanks and peace all👍🏻

2nd Apr 2024, 8:42 PM
Ансат Жумагалиев
Ансат Жумагалиев - avatar
6 Respuestas
+ 4
Ансат Жумагалиев in mathematics, have you used log(x), sine(x), cosine(x), tangent(x)... etc.? Then you have worked with functions. Functions take input parameters (such as x), and return a computed value (y) based on the input. Python's def statement (def is short for "define") is a way to define any such function in your program. It doesn't execute until you call it. def log(x): #compute logarithm of x . . . return y # return the computed value #main code c = log(100) # now c holds the return value print(c) Python functions are versatile, as they may have many input parameters, or none at all. They may return one or more values, or none at all. And they are not necessarily mathematical. Functions can do other processing, too, such as string manipulation. Are there some areas about function definitions that need further explanation? Please follow up and ask. The Sololearn community has a number of good explainers who can present different angles and help you understand.
2nd Apr 2024, 9:14 PM
Brian
Brian - avatar
+ 3
Brian , I learned that a function definition is executable. It's basically an assignment. It's what creates the function object and binds the given name to it in the local namespace, but of course, the function object itself isn't executed unless called, as you say. https://docs.python.org/3/reference/compound_stmts.html#function-definitions [Edit: I changed "binds it to the given name" to "binds the given name to it", because names refer to objects, not vice versa.]
2nd Apr 2024, 10:23 PM
Rain
Rain - avatar
+ 2
That's a good refinement to clarify what the Python interpreter does with the def statement as it reads the source code. Thank you Rain!
2nd Apr 2024, 11:00 PM
Brian
Brian - avatar
+ 1
Brian, thanks for your reply 👌🏼
2nd Apr 2024, 9:16 PM
Ансат Жумагалиев
Ансат Жумагалиев - avatar
2nd Apr 2024, 10:23 PM
Rain
Rain - avatar
+ 1
Hello! This is a basic code for the function: def(Any name you want except function)(): (indent in 4) (indent out 4) This code defines a custom function made with the base functions like "print()" and the other functions, but let's look at an example singing the happy birthday song to you. This is also my own version of it, but here it is: def the_birthday_song(): print("Happy birthday to you!") print("You are old!") print("Happy birthday to you!") print() the_birthday_song() the_birthday_song() the_birthday_song() The reason I called it three times is because I wanted to play the song 3 times, so it does the things in the def function three times. Also, remember you put def (name) then "():" after it with no spaces, and when you wanna call it, you go (name of the def function), then once again, "():" after it with no spaces. This is all I can explain. I hope you understand what I mean by this post
3rd Apr 2024, 9:46 PM
Markus
Markus - avatar