Programming Forums
User Name Password Register
 

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

Reply
 
Thread Tools Display Modes
Old Feb 25th, 2005, 1:49 PM   #1
arod199113
Programmer
 
arod199113's Avatar
 
Join Date: Feb 2005
Posts: 86
Rep Power: 0 arod199113 is an unknown quantity at this point
def my_abs()?

i was doing a tutorial a while ago and got stuck on the following part
i never finished the tutorial because i never understood the purpose for this:
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"
they said that was simpler than typing this:
a = 23
b = -23

if a < 0:
    a = -a

if b < 0:
    b = -b

if a == b:
    print "The absolute values of", a,"and",b,"are equal"
else:
    print "The absolute values of a and b are different"
i dont understand what "def" does.
i understand that the "my_abs" is just a variable and can have another name
same with "(num)"
but i dont understand the
if my_abs(a) == my_abs(b):
part
arod199113 is offline   Reply With Quote
Old Feb 25th, 2005, 5:40 PM   #2
Fred
Programmer
 
Fred's Avatar
 
Join Date: Feb 2005
Posts: 67
Rep Power: 4 Fred is on a distinguished road
The 'def' statement defines a so called function. You define it, and then you can use it with whichever numbers you want.
def my_abs(num): #num is a placeholder, so you can put in whichever 
    if num < 0:                           number you like
        num = -num
    return num

if my_abs(a) == my_abs(b):
This simply 'calls' the function. Meaning, that it is applied with the value of 'a', and then with the value of 'b' --> those were assigned to +-23.
my_abs(a) simply replaces every 'num' in the function with a 23, and then returns that value, so it can be compared to the absolute value of 'b', which is determined the same way.
I hope this helped a bit...
Fred is offline   Reply With Quote
Old Feb 28th, 2005, 6:06 PM   #3
arod199113
Programmer
 
arod199113's Avatar
 
Join Date: Feb 2005
Posts: 86
Rep Power: 0 arod199113 is an unknown quantity at this point
where it sais (num) "num" can be anything?
arod199113 is offline   Reply With Quote
Old Mar 1st, 2005, 4:23 PM   #4
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
It can. I would advise only sending numbers though.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Mar 1st, 2005, 6:42 PM   #5
arod199113
Programmer
 
arod199113's Avatar
 
Join Date: Feb 2005
Posts: 86
Rep Power: 0 arod199113 is an unknown quantity at this point
if you looked in the beginning of this thread i said that the tutorial said that the second way was easier
to me the first one is easier even if it takes up more space(since when do we worry about space saving these days anyway?)
but in other programs is "def" necessary?
arod199113 is offline   Reply With Quote
Old Mar 1st, 2005, 7:59 PM   #6
Fred
Programmer
 
Fred's Avatar
 
Join Date: Feb 2005
Posts: 67
Rep Power: 4 Fred is on a distinguished road
Quote:
Originally Posted by arod199113
in other programs is "def" necessary?
Well, it is all about code reuse...
It is not necessary, but it sure makes the job easier. For example, if you did not use a def statement to define the absolute value function, you would have to type the whole thing every time you want two numbers compared by their absolute value. It is not about space saving, but about time saving...
Fred is offline   Reply With Quote
Old Mar 2nd, 2005, 5:28 AM   #7
arod199113
Programmer
 
arod199113's Avatar
 
Join Date: Feb 2005
Posts: 86
Rep Power: 0 arod199113 is an unknown quantity at this point
i have all the time in the world
if eventualy i want to do it quicker than i will learn it
thanks thats all i realy needed to know
arod199113 is offline   Reply With Quote
Old Mar 2nd, 2005, 9:29 AM   #8
block01cube
Newbie
 
Join Date: Feb 2005
Posts: 10
Rep Power: 0 block01cube is on a distinguished road
It isn't just a matter of code reuse. If you want to harness the full object oriented power of python you will need to learn about methods and classes.

But for small programs or scripts you could get by without using def (though it would help you even then).

Cheers,

b01c
block01cube is offline   Reply With Quote
Old Mar 3rd, 2005, 11:01 AM   #9
Dietrich
Professional Programmer
 
Dietrich's Avatar
 
Join Date: Feb 2005
Posts: 434
Rep Power: 4 Dietrich is on a distinguished road
Smile

By using the right function names you can make your code more readable too. Take a look at this ...
[PHP]# a quick look at Python functions

def PieCookingStatus(CookingTime):
if CookingTime < 30:
print "Still cooking!"
elif CookingTime <= 40:
print "Almost done!"
else:
print "Take pie out of the oven!"


def PiePercentCooked(CookingTime):
return 100*CookingTime/40


# check the apple pie in the oven after 30 minutes
PieCookingStatus(30)
print "Pie is %d percent cooked." % PiePercentCooked(30);
[/PHP]
__________________
I looked it up on the Intergnats!
Dietrich is offline   Reply With Quote
Old Mar 3rd, 2005, 2:03 PM   #10
arod199113
Programmer
 
arod199113's Avatar
 
Join Date: Feb 2005
Posts: 86
Rep Power: 0 arod199113 is an unknown quantity at this point
so "def" gives another name to a variable?
arod199113 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




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

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