Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Python (http://www.programmingforums.org/forum43.html)
-   -   Defining a Function (http://www.programmingforums.org/showthread.php?t=12124)

m0rb1d Dec 7th, 2006 9:32 AM

Defining a Function
 
Heh, I seem to be all over this forum, causing myself no end of confusion trying to find a language I can bear with. Given enough time I"ll find my niche, and only be in one forum; this is the goal anyway.

Furthermore, the same people keep answering my questions, so, as before, if you can keep your annoyance level to a minimum, I"ll get less abrasive eventually. :)

Anyway, looking over, yes, another online tutorial, I need a little clarification. I've come to the section on declaring functions, and the given example sort of throws me. It is as follows: ( with the text from the tutorial )

The program seems a little repetitive. (Programmers hate to repeat things (That's what computers are for aren't they?)) Fortunately Python allows you to create functions to remove duplication. Here is the rewritten example:

:

a = 23
b = -23

def my_abs(num):
    if num < 0:
        num = -num
    return num

if my_abs(a) == my_abs(b):
    print "The absolute values of", a,"and",b,"are equal"
else:
    print "The absolute values of a and b are different"


I understand what it's doing, but now the how of it. Is code with Python not sequential? What's causing the confusion is the

:

def my_abs(num):
  if num < 0:
      num = -num
  return num


I see nothing declaring what (num) is. Does it get the value from the

:

if my_abs(a) == my_abs(b):

Because, it seems, following the code line by line, that the program wouldn't know what to do with the if num < 0:.

m0rb1d Dec 7th, 2006 9:34 AM

Meh, I think I get it now. After typing all that crap out, and then re-reading it, I believe I get it.

:

def my_abs(num)

Is a function, so, it's not really doing anything until

:

if my_abs(a) == my_abs(b):

So, (num) get it's value from my_abs(a), whereas the function my_abs(num) would become my_abs(a).

Sometimes I get stuck on stupid, apologies.

sixstringartist Dec 7th, 2006 9:50 AM

Im not python savvy, but I believe you are correct.

In this case, in the my_abs(a) call, my_abs is the function name and a is one argument of my_abs. This particular function only has one argument.

Once the program gets to the function, it will execute the function with the given argument. The function has no notion of which variable it was given, or where that sits in memory. Its only passed a value (in this case) which it assigns to the variable 'num'. 'num' essentially ceases to exist once the function has returned.

All of this is comeing from my knowledge of C and you probably already know most of it. I just wanted to clarify your last line:
Quote:

whereas the function my_abs(num) would become my_abs(a).
Now Im not sure if Python is different so correct me if Im wrong, but in C, my_abs would not really get the variable a, in a way that it can be altered. It only gets the value of a to use. You would have to pass the function a pointer to a if you wanted to directly alter the value of a inside the function.

btw, if you need any beginner help on C let me know.

Arevos Dec 7th, 2006 10:32 AM

Quote:

Originally Posted by m0rb1d (Post 120759)
So, (num) get it's value from my_abs(a), whereas the function my_abs(num) would become my_abs(a).

Sometimes I get stuck on stupid, apologies.

I've found that most newcomers to programmers have a lot of difficulty with the concept of functions, particularly with how parameters are passed to functions. So to actually grasp the essence of functions so quickly is far from stupid.

Quote:

Originally Posted by sixstringartist
Now Im not sure if Python is different so correct me if Im wrong, but in C, my_abs would not really get the variable a, in a way that it can be altered. It only gets the value of a to use. You would have to pass the function a pointer to a if you wanted to directly alter the value of a inside the function.

Don't confuse him ;)

In Python, all variables are references, but due to Python's shallow learning curve, you don't really need to know about this until you start dealing with objects and mutable data structures. So one step at a time.

Sane Dec 7th, 2006 3:39 PM

Quote:

Originally Posted by Arevos (Post 120764)
In Python, all variables are references

I'm confused... aren't integers, floats, strings, etc... all copied? Not referenced? And only objects and such are referenced?

Arevos Dec 7th, 2006 4:02 PM

Quote:

Originally Posted by Sane (Post 120789)
I'm confused... aren't integers, floats, strings, etc... all copied? Not referenced? And only objects and such are referenced?

No, in Python, everything is an object. There's no such thing as a primitive. However, numbers and strings are immutable, so they act in a similar fashion to the way primitives behave in languages like Java or C.

Take the following Python code:
:

  1. x = 10
  2. y = 10
  3.  
  4. x == y  # True
  5. x is# True

And the following C code:
:

  1. int x = 10;
  2. int y = 10;
  3.  
  4. x == y    /* true */
  5. &x == &y  /* false */


In C, x and y would hold the same value, but they'd occupy different slots in memory, so "&x == &y" is false. In Python, both x and y reference the same immutable object; not only is "x == y" true, but "x is y" is true as well.

I guess I should add that this is mostly just advanced trivia, so if m0rb1d or any other newcomer to Python finds talk of references and immutable objects confusing; don't worry about it. Knowing the difference between primitives and Python's implementation of numbers and strings has little practical value.

Sane Dec 7th, 2006 6:19 PM

But... with respect to passing to a function? An integer is copied, since its value can only be modified within the scope. No?

DaWei Dec 7th, 2006 7:30 PM

Arevos' point is that it isn't a copy, it's a reference to an immutable value, thus, if you reassign it in the function, you're reassigning the reference to another object, not reassigning the value of the original object. Consider this:
:

>>> def boogie (a):
...    print a
...    a = 5
...    print a
...    return a
...
>>> a = 10
>>> print boogie (a)
10
5
5
>>> a
10
>>>


Dietrich Dec 8th, 2006 1:27 AM

Quote:

Originally Posted by DaWei (Post 120801)
Arevos' point is that it isn't a copy, it's a reference to an immutable value, thus, if you reassign it in the function, you're reassigning the reference to another object, not reassigning the value of the original object. Consider this:
:

>>> def boogie (a):
...    print a
...    a = 5
...    print a
...    return a
...
>>> a = 10
>>> print boogie (a)
10
5
5
>>> a
10
>>>


In other words, the variable 'a' in the function stays local to the function, as long as variable 'a' is immutable like a number, string or tuple. Immutable parameters are passed by value.

Arevos Dec 8th, 2006 3:58 AM

Quote:

Originally Posted by Dietrich (Post 120829)
In other words, the variable 'a' in the function stays local to the function, as long as variable 'a' is immutable like a number, string or tuple. Immutable parameters are passed by value.

No, that's not it. The only difference between a normal object and an immutable one, is that an immutable object cannot be altered in any way. There is no difference in the way immutable parameters are passed to functions. Python never passes arguments by value, only by reference.

Only the reference stays local to the function. When one adds to or multiplies a variable, all you're doing is changing the local reference to point at another number. As DaWei says:
Quote:

Originally Posted by DaWei
it isn't a copy, it's a reference to an immutable value, thus, if you reassign it in the function, you're reassigning the reference to another object, not reassigning the value of the original object.

For instance, if you reference the number 10 in one function, and the number 10 in a completely different function in a completely different module, both references point to exactly the same object. You cannot copy or clone an immutable object like a number or a string; there will never be more than one "10" object in memory.


All times are GMT -5. The time now is 12:46 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC