![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Nov 2006
Posts: 19
Rep Power: 0
![]() |
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 numI 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:. |
|
|
|
|
|
#2 |
|
Newbie
Join Date: Nov 2006
Posts: 19
Rep Power: 0
![]() |
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. |
|
|
|
|
|
#3 | |
|
Programmer
Join Date: Jun 2005
Posts: 68
Rep Power: 4
![]() |
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:
btw, if you need any beginner help on C let me know. |
|
|
|
|
|
|
#4 | ||
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5
![]() |
Quote:
Quote:
![]() 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. |
||
|
|
|
|
|
#5 |
|
Programming Guru
![]() |
I'm confused... aren't integers, floats, strings, etc... all copied? Not referenced? And only objects and such are referenced?
|
|
|
|
|
|
#6 | |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5
![]() |
Quote:
Take the following Python code: python Syntax (Toggle Plain Text)
C Syntax (Toggle Plain Text)
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. |
|
|
|
|
|
|
#7 |
|
Programming Guru
![]() |
But... with respect to passing to a function? An integer is copied, since its value can only be modified within the scope. No?
|
|
|
|
|
|
#8 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
|
|
#9 | |
|
Professional Programmer
Join Date: Feb 2005
Posts: 434
Rep Power: 4
![]() |
Quote:
__________________
I looked it up on the Intergnats! |
|
|
|
|
|
|
#10 | ||
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5
![]() |
Quote:
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:
|
||
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
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 |