def function | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

def function

Can someone help me please? What is the difference between the items contained within the parentheses? Example: def test (x) or def test (x, y) What is the difference between these two functions? Do these elements in parenthesis have any meaning, or is it just the expression on the bottom line that matters? def test(x): return x * x def test(x, y): return x * y

31st Dec 2017, 6:30 PM
Biologia
3 Answers
+ 1
These paranthesis are the values these functions will get through main or user's input. In first case, i.e., def test(x), this function need only 1 value to work. As it's returning x*x which is value's square. But in second case, i.e., def test(x,y), this function will need exact two values to work. Like multiplying two values or may be addition or subtraction of two values. For eg, if i wanna create a function which add two numbers and compares if the result is higher or lower than the 3rd one. So that function will need exact 3 values for sure.
31st Dec 2017, 6:50 PM
Harjeet Singh
Harjeet Singh - avatar
+ 1
As for test(x), you can use only one input. e.g. def test(x): return x*x print(test(3)) // the output is 9 As for test(x,y) you can use two inputs. e.g. def test(x,y): return x*y print(test(2,3)) // the output is 6 Other names are allowed for inputs. e.g. def test(a,b,c): return a+b+c print(test(1,2,3)) // the output is 6
31st Dec 2017, 7:02 PM
shouta yui
+ 1
tks! ✌️
8th Feb 2018, 11:22 AM
Biologia