Guys please I don't seem to fully understand how def functions work in python | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Guys please I don't seem to fully understand how def functions work in python

28th Apr 2018, 7:30 PM
CodeX
CodeX - avatar
18 Answers
+ 2
You can make functions by the def statement: 1.def CustomFunction(M, N, O): 2. P = (M * N / O) 3. if P == 2: 4. return(M - N + O) 5. else: 6. return(P) 7. 8.print(CustomFunction(4, 3, 6)) 9. >>>>>>>>>>>>>>>>>>>> Result: 7 Explanation: CustomFunction takes 3 arguments from the function call (from 8th line in example), in previous example it took M as 4, N as 3 and O as 6. Inside function definition it calculated the argument M(4) to be multiplied by N(3) and M's and N's income (12) was divided by O(6) so P inside the function is 2, if P was 2, it returned M - N + O to the CustomFunction call in 8th line, then it printed M - N + O, what is 7. If this didn't help, I suggest you to just repeat the module about function definitons and try it out on the Python 3 playground until you can. For me it made function definition full clear, when I knew, what was function in Python.
28th Apr 2018, 8:29 PM
Seb TheS
Seb TheS - avatar
+ 2
def keyword is used for inform parser that your goi g to define one function or an object function and you must write it at start of definition: def simpleFunc(): print("I am simpleFunc") This define a simpleFunc function
28th Apr 2018, 7:37 PM
KrOW
KrOW - avatar
+ 2
return is really useful only inside a function for exit to it and return to code that called it a value (but it can return nothing also)
28th Apr 2018, 7:48 PM
KrOW
KrOW - avatar
+ 2
thanks KrOW
28th Apr 2018, 9:01 PM
CodeX
CodeX - avatar
+ 2
👍👍👍 I edited your code with explanations comments https://code.sololearn.com/cxnJJ3E95hw8/?ref=app
28th Apr 2018, 9:05 PM
KrOW
KrOW - avatar
+ 1
thanks there's a simple project I'm working on.... to calculate two scores using division and addition... lemme get the code and share
28th Apr 2018, 7:38 PM
CodeX
CodeX - avatar
+ 1
or lemme just tell you where I'm stuck at
28th Apr 2018, 7:39 PM
CodeX
CodeX - avatar
+ 1
I don't full understand the return statement
28th Apr 2018, 7:41 PM
CodeX
CodeX - avatar
+ 1
Return statement is simple to understand after few examples: Think about the len function, for len("Hello") the len function RETURNS 5. print(len("hello")) == print(5) def example("A"): return(A ** 2) print(example(10)) == print(100) result: True
28th Apr 2018, 8:35 PM
Seb TheS
Seb TheS - avatar
+ 1
thanks guys
28th Apr 2018, 9:00 PM
CodeX
CodeX - avatar
+ 1
thanks Seb TheS your explanation was very clear to me
28th Apr 2018, 9:01 PM
CodeX
CodeX - avatar
+ 1
wow, that's really cool I just check it out.... seems more complex than what I did
28th Apr 2018, 9:35 PM
CodeX
CodeX - avatar
+ 1
CodeX It seem but its not... Read comments and try to figure out that do every statement
28th Apr 2018, 10:00 PM
KrOW
KrOW - avatar
+ 1
okay thanks ....y'all are the best
28th Apr 2018, 10:01 PM
CodeX
CodeX - avatar
+ 1
getOperands function return a list if 2 float elements when op1 get the first (operand[0]) while op2 the second (operand[1])... if you dont know what is a list then dont take care for now, think only that they store currents operands
28th Apr 2018, 10:05 PM
KrOW
KrOW - avatar
0
a little tho
28th Apr 2018, 9:36 PM
CodeX
CodeX - avatar
0
a little tho
28th Apr 2018, 9:42 PM
CodeX
CodeX - avatar
0
I didn't understand when you used: result = 0 op1 = operand[0] op2 = operand[1]
28th Apr 2018, 9:45 PM
CodeX
CodeX - avatar