Programming Forums
User Name Password Register
 

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

Closed Thread
 
Thread Tools Display Modes
Old Jun 15th, 2006, 10:48 AM   #11
Jessehk
The Oblivious One
 
Jessehk's Avatar
 
Join Date: May 2005
Location: Ontario, Canada
Posts: 644
Rep Power: 4 Jessehk is on a distinguished road
Quote:
Originally Posted by Prm753
I think Jessehk's example is one of the best smaller-sized examples about functions that I have seen.
*starting to blush*
:o
__________________
Dr. Zoidberg: [ecstatic] I'm going to a movie... with FRIENDS!
Jessehk is offline  
Old Jun 15th, 2006, 11:58 AM   #12
Zephirus
Newbie
 
Zephirus's Avatar
 
Join Date: May 2006
Posts: 12
Rep Power: 0 Zephirus is on a distinguished road
Wow thanks guys. I haven't had time to read all this since I am at work at the moment. To Dewei, yes, in Algebra we did things like f(x) 2x+3 where you had to find x= a number.

Ill read all this tonight. Thanks for the help everyone.
Zephirus is offline  
Old Jun 15th, 2006, 4:00 PM   #13
Zephirus
Newbie
 
Zephirus's Avatar
 
Join Date: May 2006
Posts: 12
Rep Power: 0 Zephirus is on a distinguished road
Ok reading over things... let me try. The syntax will be off cause im new to this but still...



#include <iostream>

int addtwo(int)

int main()
{
cout << "Enter a number" << endl;
int firstnumber;
cin >> firstnumber;
cout << "That number plus 2 is " << addtwo(firstnumber) << endl;
}


int addtwo(int numberinput)
{
return numberinput + 2;
}



Right?
Zephirus is offline  
Old Jun 15th, 2006, 4:05 PM   #14
Jimbo
Battle Programmer
 
Jimbo's Avatar
 
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 763
Rep Power: 3 Jimbo is on a distinguished road
Looks good, although the function prototype should have a semi-colon after it and you should declare the scope of cin, cout, and endl (either prepend std:: to all of them, or put using statements after the #include). By the way, when you post code here, put it in [code] tags and it'll preserve formatting and keep it easier to read for everyone.
Jimbo is offline  
Old Jun 15th, 2006, 5:38 PM   #15
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
What Jim said. You wanna join the club and get the free goodies, read the freakin' rules.
__________________
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  
Old Jun 15th, 2006, 5:46 PM   #16
Zephirus
Newbie
 
Zephirus's Avatar
 
Join Date: May 2006
Posts: 12
Rep Power: 0 Zephirus is on a distinguished road
Quote:
Originally Posted by DaWei
What Jim said. You wanna join the club and get the free goodies, read the freakin' rules.

I came here for help, I didn't ask for a childish remark for a 90 year old man on Viagra. Thank you.
Zephirus is offline  
Old Jun 15th, 2006, 5:49 PM   #17
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 1,886
Rep Power: 5 Sane will become famous soon enough
Send a message via MSN to Sane
Wow, that was an excellent explanation of functions Jessehk. I'm surprised. If only the tutorial I read when I was first introduced to functions a while back was that well explained. The tutorial I read before seemed to assume I had learned Functions in math before. However, you explained it in a realistic context.

Perhaps you could have explained more about the purpose of void though.
Sane is offline  
Old Jun 15th, 2006, 5:51 PM   #18
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
The purpose of 'void' is to explicitly state that NOTHING is in play. This precludes trying to decide if something is missing or left out or if its absence is intended.
__________________
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  
Old Jun 15th, 2006, 6:01 PM   #19
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 1,886
Rep Power: 5 Sane will become famous soon enough
Send a message via MSN to Sane
However... I understand that void is generally bad practice. I've learned to return (0); instead of voiding a function. Is there any information I am missing, or different sides to that rule of thumb?
Sane is offline  
Old Jun 15th, 2006, 6:27 PM   #20
nnxion
Programming Guru
 
nnxion's Avatar
 
Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5 nnxion is on a distinguished road
Quote:
Originally Posted by Sane
However... I understand that void is generally bad practice. I've learned to return (0); instead of voiding a function. Is there any information I am missing, or different sides to that rule of thumb?
Having void functions is fine, but note that almost everything in the C standard library returns whether it failed or succeeded. In languages such as C# this is not the case, that's because if something failed an exception would be raised. Of course if the power went out, the computer is off, and it's likely you'd be able to tell in some kind of manner.
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for."
-- Socrates
nnxion is offline  
Closed Thread

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 11:39 AM.

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