Python Forum

Full Version: Def - Return Question
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi Everyone,

I'm trying to understand the below code. My question is why is it returning salary_bins?

So from what I understand it first defines the table columns, creates the bins and then draws the histograms.

But the return part I'm not sure I fully understand. I tried to run it with return salary_bins and it created the histograms as well.

Thanks in advance.

def histograms(t):
    ages = t.column('Age')
    salaries = t.column('Salary')
    age_bins = np.arange(min(ages),max(ages)+1,1)
    salary_bins = np.arange(min(salaries), max(salaries)+1000000,1000000)
    t.hist('Age', bins=age_bins, unit='year')
    t.hist('Salary', bins=salary_bins, unit='$')
    return age_bins # Keep this statement so that your work can be checked
    
histograms(full_data)
print('Two histograms should be displayed below')
The return statement does just that - returns selected the result of the processing within a function.

So, if you don't need any of the data produced in a function, you are not obligated to use it at at all - by default, functions return None in Python

You also don't have to use the returned value in the calling code - you may have a function that returns result, and sometimes this result is discarded.
Hi volcano63,

Thanks for the explanation, can I please clarify. So when I call the function histograms(full_data), in addition to the histograms it will give me the value of age_bins? I actually don't need to return anything here and still get the histograms drawn?
(Jul-01-2018, 11:04 AM)tryingtolearnpython Wrote: [ -> ]I actually don't need to return anything here and still get the histograms drawn?
If it worked when you return value - it should work even without it.
thank you very much volcano63