Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python function execution [closed]

Tags:

python

So i found this site called codewars.com and wanted to signup. When i selected my language as python, it gave me a problem to solve:

The code does not execute properly. Try to figure out why.

def multiply(a, b):
  a * b

I am not able to figure out why. It executes correctly in PyCharm using python 3.4 when i added print(a*b) instead of a*b and when i called the function using multiply(2,3). The code is also being successfully executed using just the given snippet. It has been given that the above python code is in 2.7 Any ideas?

like image 747
RaviTej310 Avatar asked Dec 28 '15 11:12

RaviTej310


2 Answers

If it is a function, it needs to return something. Otherwise, running it is kind of useless.

So you probably need to say:

def multiply(a, b):
  return a * b

You probably want to read more about functions in Python and when this would make sense (passing by reference, for example). This can be a good starting point: Python functions.

like image 88
fedorqui 'SO stop harming' Avatar answered Nov 03 '22 00:11

fedorqui 'SO stop harming'


There is no return value, the code will be OK

def multiply(a, b):
   return a * b
like image 36
yanghaogn Avatar answered Nov 02 '22 23:11

yanghaogn