+ 1

Functions

I'm trying to learn the functions lesson in python but I understood nothing and just skipped it can someone help

5th Jul 2024, 11:28 PM
Repear
Repear - avatar
7 ответов
+ 4
To make your own function, use the "def" keyword. Try to think as we speak in English. define my_function which takes (some arguments): it will do this and do that it can print() something and return some value to the caller Now in real code: def my_function(arguments): x = arguments * 2 x = x * 10 print(arguments) return x my_var = my_function(10) It prints 10 on screen because of "print(arguments)" print(my_var) # It prints 200 because my_var called my_function with an argument 10, x = 10 * 2 becomes 20, and x * 10 = 200 and x got returned (assign to my_var) in the end.
6th Jul 2024, 4:11 AM
Wong Hei Ming
Wong Hei Ming - avatar
+ 4
The Sololearn sandbox is very odd, but something useful for analogy is that it treats the entire Python script like a function. In a regular Python interpreter (which I recommend that you use if you can), inputs don’t have to all be entered at the beginning of a program, and print statements can be run at any time as well. In the Sololearn sandbox, you pass all the inputs at the beginning, and you have the print statement spit out the result at or near the end. Functions are like that. When you call them, you pass them a bunch of arguments, then they work off in their own little scope, and when they’re done, they often return a result. squared = pow(number, 2) This calls built-in function pow, which takes two number arguments, and returns the first number to the power of the second. If the pow function wasn’t built in and you were defining it yourself instead, the code might look like: def pow(num, p): return num ** p After a function is defined, you can call it.
6th Jul 2024, 12:21 AM
Wilbur Jaywright
Wilbur Jaywright - avatar
+ 2
The ** operator means “to the power of”, which I did not explain, but was there something else?
6th Jul 2024, 1:38 AM
Wilbur Jaywright
Wilbur Jaywright - avatar
+ 1
Realy thanks
6th Jul 2024, 12:23 AM
Repear
Repear - avatar
+ 1
But I didn't get the part about making my own function
6th Jul 2024, 12:24 AM
Repear
Repear - avatar
+ 1
Yes my dear,look at this code to understand all in Function: https://sololearn.com/compiler-playground/cCrFc1Z9lKSp/?ref=app
6th Jul 2024, 6:23 PM
Yazan Alhallack
Yazan Alhallack - avatar
0
Thx so much guys
7th Jul 2024, 6:20 PM
Repear
Repear - avatar