+ 1
Guys please I don't seem to fully understand how def functions work in python
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.
+ 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
+ 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)
+ 2
thanks KrOW
+ 2
👍👍👍 I edited your code with explanations comments
https://code.sololearn.com/cxnJJ3E95hw8/?ref=app
+ 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
+ 1
or lemme just tell you where I'm stuck at
+ 1
I don't full understand the return statement
+ 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
+ 1
thanks guys
+ 1
thanks Seb TheS your explanation was very clear to me
+ 1
wow, that's really cool I just check it out.... seems more complex than what I did
+ 1
CodeX It seem but its not... Read comments and try to figure out that do every statement
+ 1
okay thanks ....y'all are the best
+ 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
0
a little tho
0
a little tho
0
I didn't understand when you used:
result = 0
op1 = operand[0]
op2 = operand[1]