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

Carrying values over in a function

P: 5
I'm pretty new to python, and I'm currently having problems carrying a value inside a function, to be used again by another function.

I'm not sure if that make sense, and I don't really have a good simple example, sorry.

And since I'm totally new to programming I might just be misunderstanding what a function really does.
Nov 20 '07 #1
Share this Question
Share on Google+
3 Replies


bartonc
Expert 5K+
P: 6,596
I'm pretty new to python, and I'm currently having problems carrying a value inside a function, to be used again by another function.

I'm not sure if that make sense, and I don't really have a good simple example, sorry.

And since I'm totally new to programming I might just be misunderstanding what a function really does.
I hope this helps with some basic understanding of the way functions work:
Expand|Select|Wrap|Line Numbers
  1. # functions receive values in the parameters given in the fuction call
  2. # and return values that they produce using the return statement
  3.  
  4. def Add(x, y):
  5.     return x + y
  6.  
  7. def Multiply(a, b):
  8.     c = a * b
  9.     return c
  10.  
  11. d = Add(1, 2)
  12. print d
  13.  
  14. e = Multiply(d, 6)
  15. print e
Nov 20 '07 #2

P: 5
I hope this helps with some basic understanding of the way functions work:
Expand|Select|Wrap|Line Numbers
  1. # functions receive values in the parameters given in the fuction call
  2. # and return values that they produce using the return statement
  3.  
  4. def Add(x, y):
  5.     return x + y
  6.  
  7. def Multiply(a, b):
  8.     c = a * b
  9.     return c
  10.  
  11. d = Add(1, 2)
  12. print d
  13.  
  14. e = Multiply(d, 6)
  15. print e
Yes, that really helped thanks, program works fine now : D.

I didn't actually know you could assign a variable to a function.
Nov 20 '07 #3

bartonc
Expert 5K+
P: 6,596
Yes, that really helped thanks, program works fine now : D.

I didn't actually know you could assign a variable to a function.
I remember programming in Pascal. Back then we were force to make a distinction between functions and procedures. For Python this is not so. But there some little pieces of trivia that is good to know...
  1. When a function (or method of an object) does not explicitly return a value, Python automatically returns None.
  2. Even if a function returns a value you are not require to assign it to a variable.
Nov 21 '07 #4

Post your reply

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