![]() |
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 = 23I 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):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:. |
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. |
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. |
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. |
Quote:
|
Quote:
Take the following Python code: :
:
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. |
But... with respect to passing to a function? An integer is copied, since its value can only be modified within the scope. No?
|
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): |
Quote:
|
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:
|
| 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