Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Dec 7th, 2006, 9:32 AM   #1
m0rb1d
Newbie
 
Join Date: Nov 2006
Posts: 19
Rep Power: 0 m0rb1d is on a distinguished road
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 is offline   Reply With Quote
Old Dec 7th, 2006, 9:34 AM   #2
m0rb1d
Newbie
 
Join Date: Nov 2006
Posts: 19
Rep Power: 0 m0rb1d is on a distinguished road
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.
m0rb1d is offline   Reply With Quote
Old Dec 7th, 2006, 9:50 AM   #3
sixstringartist
Programmer
 
Join Date: Jun 2005
Posts: 68
Rep Power: 4 sixstringartist is on a distinguished road
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.
sixstringartist is offline   Reply With Quote
Old Dec 7th, 2006, 10:32 AM   #4
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5 Arevos is on a distinguished road
Quote:
Originally Posted by m0rb1d View Post
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.
Arevos is offline   Reply With Quote
Old Dec 7th, 2006, 3:39 PM   #5
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 1,887
Rep Power: 5 Sane will become famous soon enough
Send a message via MSN to Sane
Quote:
Originally Posted by Arevos View Post
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?
Sane is offline   Reply With Quote
Old Dec 7th, 2006, 4:02 PM   #6
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5 Arevos is on a distinguished road
Quote:
Originally Posted by Sane View Post
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:
python Syntax (Toggle Plain Text)
  1. x = 10
  2. y = 10
  3.  
  4. x == y # True
  5. x is y # True
And the following C code:
C Syntax (Toggle Plain Text)
  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.
Arevos is offline   Reply With Quote
Old Dec 7th, 2006, 6:19 PM   #7
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 1,887
Rep Power: 5 Sane will become famous soon enough
Send a message via MSN to Sane
But... with respect to passing to a function? An integer is copied, since its value can only be modified within the scope. No?
Sane is offline   Reply With Quote
Old Dec 7th, 2006, 7:30 PM   #8
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
>>>
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Dec 8th, 2006, 1:27 AM   #9
Dietrich
Professional Programmer
 
Dietrich's Avatar
 
Join Date: Feb 2005
Posts: 434
Rep Power: 4 Dietrich is on a distinguished road
Quote:
Originally Posted by DaWei View Post
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.
__________________
I looked it up on the Intergnats!
Dietrich is offline   Reply With Quote
Old Dec 8th, 2006, 3:58 AM   #10
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5 Arevos is on a distinguished road
Quote:
Originally Posted by Dietrich View Post
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.
Arevos is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Compiling Maverik 6.2 (from C) megamind5005 C 16 May 3rd, 2006 5:41 PM
libraries matko C 1 Jan 22nd, 2006 2:12 PM
Php Postgresql Class Pizentios Show Off Your Open Source Projects 15 Jun 28th, 2005 9:55 AM
Jackpot game zorin Visual Basic 3 Jun 10th, 2005 1:19 PM
airport Log program using 3D linked List : problem reading from file gemini_shooter C++ 0 Mar 2nd, 2005 4:12 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 7:12 PM.

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