what is the return type of the function? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

what is the return type of the function?

I am a java developer learning python, in java we are specifying return type. but in python we are not specifying any return type, I think the default return type will be string. (or) we need to do by Type casting

21st Sep 2016, 2:31 AM
Hariprasad Rajaram
Hariprasad Rajaram - avatar
3 Answers
+ 3
def add (x, y): z = x + y if type(x, y) == int: return int(z) elif type(x, y) == str: return str(z) elif type(x, y) == float: return float(z) elif type(x, y) == list: return list(z) elif type(x, y) == dict: return dict(z) add (2, 3) z = int(5) add('2', '3') z = str('23') etc... this is just an example code, but it should represent how return specifies the type it returns (as you can't operate on two different types).
21st Sep 2016, 5:15 AM
vyavas
+ 2
python is a dynamically typed language, so you wont find the typical int x = 6, for example. you dont need to specify return types.
24th Sep 2016, 7:28 PM
Andrés Quintero
Andrés Quintero - avatar
0
There is no default return type in python like we have in Java / php. Check this out def checkDefaultReturnType(): print("hello") print(type(checkDefaultReturnType())) It prints "<class 'NoneType'> " against print(type(checkDefaultReturnType()))
25th Sep 2016, 12:40 PM
Ashutosh Narayan jha
Ashutosh Narayan jha - avatar