By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,171 Members | 809 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,171 IT Pros & Developers. It's quick & easy.

Is it possible to return variables to the main function?

P: 3
Hi, I'm new to python, programming in general actually. I was wondering if it was possible to return variables from a function? Hmm that probably didn't make sense, i'll give an example. Lets say i had this program:
Expand|Select|Wrap|Line Numbers
  1. def main ():
  2.     age = input ('What is your age in years? ')
  3.     weeksOld = age * 52
  4.     print 'Wow, you are at least ', weeksOld, ' weeks old!!!'
  5. main ()
  6.  
Is it possible to separate the calculation into a function away from main and still have it return the variable for the print statement? I'll try and show you what i mean:
Expand|Select|Wrap|Line Numbers
  1. def main ():
  2.     age = input ('What is your age in years? ')
  3.     weeksOld (age)
  4.     print 'Wow, you are at least ', wsOld, ' weeks old!!!'
  5.  
  6. def weeksOld (years):
  7.     wsOld = years * 52
  8.     return wsOld
  9. main ()
  10.  
Sorry if that was hard to understand or if the question was rather nooby, as i said i'm new so try not to flame me too much, thanks!
Mar 18 '11 #1

✓ answered by Mariostg

Cameron,
Did you try it? You could answer this by yourself :)

Expand|Select|Wrap|Line Numbers
  1. def weeksOld (years): 
  2.     return = years * 52 
  3. main ()
  4.  

Share this Question
Share on Google+
2 Replies


100+
P: 332
Cameron,
Did you try it? You could answer this by yourself :)

Expand|Select|Wrap|Line Numbers
  1. def weeksOld (years): 
  2.     return = years * 52 
  3. main ()
  4.  
Mar 18 '11 #2

P: 3
Haha that was actually pretty simple :), thanks heaps!
Mar 18 '11 #3

Post your reply

Sign in to post your reply or Sign up for a free account.