Carrying values over in a function

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • aLiase
    New Member
    • Nov 2007
    • 5

    Carrying values over in a function

    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 misunderstandin g what a function really does.
  • bartonc
    Recognized Expert Expert
    • Sep 2006
    • 6478

    #2
    Originally posted by aLiase
    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 misunderstandin g what a function really does.
    I hope this helps with some basic understanding of the way functions work:[CODE=python]# functions receive values in the parameters given in the fuction call
    # and return values that they produce using the return statement

    def Add(x, y):
    return x + y

    def Multiply(a, b):
    c = a * b
    return c

    d = Add(1, 2)
    print d

    e = Multiply(d, 6)
    print e[/CODE]

    Comment

    • aLiase
      New Member
      • Nov 2007
      • 5

      #3
      Originally posted by bartonc
      I hope this helps with some basic understanding of the way functions work:[CODE=python]# functions receive values in the parameters given in the fuction call
      # and return values that they produce using the return statement

      def Add(x, y):
      return x + y

      def Multiply(a, b):
      c = a * b
      return c

      d = Add(1, 2)
      print d

      e = Multiply(d, 6)
      print e[/CODE]
      Yes, that really helped thanks, program works fine now : D.

      I didn't actually know you could assign a variable to a function.

      Comment

      • bartonc
        Recognized Expert Expert
        • Sep 2006
        • 6478

        #4
        Originally posted by aLiase
        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.

        Comment

        Working...