|
-
Mar 13th, 2003, 05:31 PM
#1
Thread Starter
Hyperactive Member
I hate functions!
Yes, it's official.. I hate them.. I don't know how to write them, and worse, i dont get how to call them from main()
I'm supposed to write a program that asks you for a month/day/year and then using a function, it's supposed to return the day it it of the year... (from 1 to 366) so I'm totaly lost
-Emo
-=VB6 Enterprise Edition=-
-=VC++6Enterprise Edition=-
«¤E³m°O²™¤»
-
Mar 13th, 2003, 05:56 PM
#2
Frenzied Member
Ok relax mate Functions are not that hard at all once you understand them
their basic syntax is like this:
Code:
return_type FunctionName (arg_type arg1, arg_type arg2)
Here is a simple function that accepts an int, multiplies it with 2 and returns the result to the caller function (in this case main)
Code:
int Multi(int i){
return (i * 2);
}
int main(){
int answer = Multi(2);
return 0;
}
At the end of this execution, the variable answer holds 4.
If you want to make a function that does not return anything, specify void as the returntype.
If you don't need any arguments, leave the list empty
Instead of leaving the list empty you can also specify void (this is needed in C, but not in Standard C++)
Code:
void MyFunc(void) // also valid, but Not needed
This is the simple explanation, if you need more help, let us know 
Oh and also, if you want to call a function that's defined below main you need to specify a prototype.
this won't work for example:
Code:
int main(){
int answer = Multi(2);
return 0;
}
int Multi(int i){
return (i * 2);
}
Because the compiler doesn't know what multi is, since its not known yet when main is being executed (since the Multi is below main) To overcome this problem you should write a prototype. This is simply the function specification without the actual content of the function (the stuff between { and } )
This will work:
Code:
int Multi(int i); //Prototype for function Multi
int main(){
int answer = Multi(2);
return 0;
}
int Multi(int i){
return (i * 2);
}
Good luck, and remember, functions aren't that hard at all
Jop - validweb.nl
Alcohol doesn't solve any problems, but then again, neither does milk.
-
Mar 13th, 2003, 06:57 PM
#3
Thread Starter
Hyperactive Member
wow, what a reply... thanks a lot..that cleared out a lot of things.... 
right now I'm in class and tonight we'll learn Pointers from what I read so far, they're not that hard.. but I'll see if I'm wrong
-Emo
-=VB6 Enterprise Edition=-
-=VC++6Enterprise Edition=-
«¤E³m°O²™¤»
-
Mar 13th, 2003, 07:39 PM
#4
Originally posted by Emo
right now I'm in class and tonight we'll learn Pointers from what I read so far, they're not that hard..
*Evil Laugh*
Every passing hour brings the Solar System forty-three thousand miles closer to Globular Cluster M13 in Hercules -- and still there are some misfits who insist that there is no such thing as progress.
-
Mar 13th, 2003, 07:56 PM
#5
Thread Starter
Hyperactive Member
Originally posted by sunburnt
*Evil Laugh*
ok...now I second that... but to me, they were way easier to get than the fuctions
-Emo
-=VB6 Enterprise Edition=-
-=VC++6Enterprise Edition=-
«¤E³m°O²™¤»
-
Mar 13th, 2003, 09:06 PM
#6
Addicted Member
i almost wet my pants when you said pointers seem easier than functions. are you sure you're doing c++?
I always find pointers to be a thorn in my ass, and it always seems that as soon as i think i understand how to use them, i do something that gets another error.
-
Mar 13th, 2003, 09:20 PM
#7
Thread Starter
Hyperactive Member
hahahah, actually I'm doing C because it's a pre-requisite to the C++ class I want to take here at my College...
but yes, after going over, fuctions are a pain to code compared to pointers
-Emo
-=VB6 Enterprise Edition=-
-=VC++6Enterprise Edition=-
«¤E³m°O²™¤»
-
Mar 13th, 2003, 09:39 PM
#8
Frenzied Member
This might also help you, though there are some C++ specific things in there...
http://www.warwick.ac.uk/~esubad/functions.php
Z.
-
Mar 14th, 2003, 02:00 AM
#9
Monday Morning Lunatic
I really really REALLY need to get my arse in gear RE that tutorial...
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Mar 14th, 2003, 08:00 AM
#10
Frenzied Member
Hehe I had to laugh at this one:
Now it is time for an example. Many tutorials would start you off with something like a multiplication function, or an addition function. I think that those are a waste of a function, and can't show some of the variety that can exist in functions. We are going to calculate the distance between two points for our first function. The formula is: (...)
float dist(float Ax, float Ay, float Bx, float By) {
return sqrt(((Bx - Ax) * (Bx-Ax)) + ((By - Ay) * (By - Ay)));
}
That doesn't really makes clear what a function itself is, you just puzzle people by looking at complex looking parentheses and variables.
Anyway,
I think that those are a waste of a function, and can't show some of the variety that can exist in functions.
Please note that a function is just a specific block of code (main is a function also) so you can put almost anything in it.
Jop - validweb.nl
Alcohol doesn't solve any problems, but then again, neither does milk.
-
Mar 14th, 2003, 01:16 PM
#11
I prefer parksie's approach, but I haven't read this thing in a while. If a good explanation follows it is better IMO - but then I can't see it from the point of a newbie.
But Jop, you're initial post contains an error, not on functions but on the way the language itself works:
Because the compiler doesn't know what multi is, since its not known yet when main is being executed (since the Multi is below main)
You should say "when main is compiled" because main won't ever get executed unless the compiler has successfully compiled the whole app.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Mar 14th, 2003, 03:25 PM
#12
Frenzied Member
Ok yeah hehe you know what I meant
Jop - validweb.nl
Alcohol doesn't solve any problems, but then again, neither does milk.
-
Mar 14th, 2003, 05:03 PM
#13
Addicted Member
I had to do the same thing
Dude i had to do the same thing for a class that i am in. Below is the code i used and it worked perfect!
/*
* Desc: Tell the day of the year
*/
/* Name: Included Files*/
#include <stdio.h>
/* Name: Leap Year Function */
int
leap(int Year)
{
/* Is it divisible by 100 & 400 or by 4 */
if(Year % 100 == 0 && Year % 400 == 0 || Year % 4 == 0 )
return(1);
else
return(0);
}
/* Name: sMonth - Structure to hold each Month's info */
typedef struct
{
int MonthNum;
int Days_N_Month;
}sMonth;
/* Name: Main Function */
int
main(void)
{
int Month; /* Month */
int Day; /* Day */
int Year; /* Year */
sMonth January; /* Declare as a month */
sMonth Febuary; /* Declare as a month */
sMonth March; /* Declare as a month */
sMonth April; /* Declare as a month */
sMonth May; /* Declare as a month */
sMonth June; /* Declare as a month */
sMonth July; /* Declare as a month */
sMonth August; /* Declare as a month */
sMonth September; /* Declare as a month */
sMonth October; /* Declare as a month */
sMonth November; /* Declare as a month */
sMonth December; /* Declare as a month */
/* Set the valuse for each month */
January.Days_N_Month = 31;
January.MonthNum = 1;
Febuary.Days_N_Month = 28;
Febuary.MonthNum = 2;
March.Days_N_Month = 31;
March.MonthNum = 3;
April.Days_N_Month = 30;
April.MonthNum = 4;
May.Days_N_Month = 31;
May.MonthNum = 5;
June.Days_N_Month = 30;
June.MonthNum = 6;
July.Days_N_Month = 31;
July.MonthNum = 7;
August.Days_N_Month = 31;
August.MonthNum = 8;
September.Days_N_Month = 30;
September.MonthNum = 9;
October.Days_N_Month = 31;
October.MonthNum = 10;
November.Days_N_Month = 30;
November.MonthNum = 11;
December.Days_N_Month = 31;
December.MonthNum = 12;
/* Get the Info needed */
printf("What is the Month, Day, and Year? (ex. January 12 1982 = 1 12 1982)\n");
scanf("%d %d %d" , &Month, &Day, &Year);
/* Test the day of the month and see if it is valid (exit the program if it is not) */
if(Day > 31 || Day < 1)
{
printf("There is not a month with %d days in it.\n",Day);
return(0);
}
/* Test for Leap Year */
if(leap(Year))
Febuary.Days_N_Month += 1;
/* See what day of the year it is */
if(Month == January.MonthNum)
printf("That is the %d day of the year.\n",Day);
else if(Month == Febuary.MonthNum)
printf("That is the %d day of the year.\n",January.Days_N_Month + Day);
else if(Month == March.MonthNum)
printf("That is the %d day of the year.\n",January.Days_N_Month + Febuary.Days_N_Month + Day);
else if(Month == April.MonthNum)
printf("That is the %d day of the year.\n",January.Days_N_Month + Febuary.Days_N_Month + March.Days_N_Month + Day);
else if(Month == May.MonthNum)
printf("That is the %d day of the year.\n",January.Days_N_Month + Febuary.Days_N_Month + March.Days_N_Month + April.Days_N_Month + Day);
else if(Month == June.MonthNum)
printf("That is the %d day of the year.\n",January.Days_N_Month + Febuary.Days_N_Month + March.Days_N_Month + April.Days_N_Month + May.Days_N_Month + Day);
else if(Month == July.MonthNum)
printf("That is the %d day of the year.\n",January.Days_N_Month + Febuary.Days_N_Month + March.Days_N_Month + April.Days_N_Month + May.Days_N_Month + June.Days_N_Month + Day);
else if(Month == August.MonthNum)
printf("That is the %d day of the year.\n",January.Days_N_Month + Febuary.Days_N_Month + March.Days_N_Month + April.Days_N_Month + May.Days_N_Month + June.Days_N_Month + July.Days_N_Month + Day);
else if(Month == September.MonthNum)
printf("That is the %d day of the year.\n",January.Days_N_Month + Febuary.Days_N_Month + March.Days_N_Month + April.Days_N_Month + May.Days_N_Month + June.Days_N_Month + July.Days_N_Month + August.Days_N_Month + Day);
else if(Month == October.MonthNum)
printf("That is the %d day of the year.\n",January.Days_N_Month + Febuary.Days_N_Month + March.Days_N_Month + April.Days_N_Month + May.Days_N_Month + June.Days_N_Month + July.Days_N_Month + August.Days_N_Month + September.Days_N_Month + Day);
else if(Month == November.MonthNum)
printf("That is the %d day of the year.\n",January.Days_N_Month + Febuary.Days_N_Month + March.Days_N_Month + April.Days_N_Month + May.Days_N_Month + June.Days_N_Month + July.Days_N_Month + August.Days_N_Month + September.Days_N_Month + October.Days_N_Month + Day);
else if(Month == December.MonthNum)
printf("That is the %d day of the year.\n",January.Days_N_Month + Febuary.Days_N_Month + March.Days_N_Month + April.Days_N_Month + May.Days_N_Month + June.Days_N_Month + July.Days_N_Month + August.Days_N_Month + September.Days_N_Month + October.Days_N_Month + November.Days_N_Month + Day);
else
printf("Month is invalid.\n");
/* Exit Program*/
return(0);
}
-
Mar 14th, 2003, 05:08 PM
#14
Addicted Member
Do you go to UAB? cause if so we might be in the same class. I am in ee130 w/ nelson
-
Mar 14th, 2003, 06:10 PM
#15
Frenzied Member
Jop: I could have thrown inline functions or macros at them (#define square(x) ...), but I figured anyone who has done any sort of mathematics knows what the parens are for 
Z.
Last edited by Zaei; Mar 14th, 2003 at 06:16 PM.
-
Mar 14th, 2003, 08:36 PM
#16
Frenzied Member
Hehe Zaei, it wasn't an offense to you (I didn't even know you wrote it ) but I in general think it's weird to say that that expression clarifies the variety of stuff that can be done in a function. But on the other hand, I think you're right about people paying attention to what they're reading should see what's the thing is about
Jop - validweb.nl
Alcohol doesn't solve any problems, but then again, neither does milk.
-
Mar 14th, 2003, 09:10 PM
#17
Frenzied Member
Originally posted by Jop
Hehe Zaei, it wasn't an offense to you (I didn't even know you wrote it ) but I in general think it's weird to say that that expression clarifies the variety of stuff that can be done in a function. But on the other hand, I think you're right about people paying attention to what they're reading should see what's the thing is about
I know it wasnt =).
Z.
-
Mar 15th, 2003, 04:28 AM
#18
Thread Starter
Hyperactive Member
Originally posted by Buy2easy.com
Do you go to UAB? cause if so we might be in the same class. I am in ee130 w/ nelson
O wow! you practically saved me!!!
your example is flawless!! thanks a lot! I owe you!
anyway, I dont know what UAB is so I doubt we go to the same class...
-Emo
-=VB6 Enterprise Edition=-
-=VC++6Enterprise Edition=-
«¤E³m°O²™¤»
-
Mar 15th, 2003, 09:00 AM
#19
Flawless?
Code:
/* Name: Leap Year Function */
int
leap(int Year)
{
/* Is it divisible by 100 & 400 or by 4 */
if(Year % 100 == 0 && Year % 400 == 0 || Year % 4 == 0 )
return(1);
else
return(0);
}
The if expression should be
Code:
if(Year % 4 == 0 && (Year % 100 != 0 || Year % 400 == 0))
The expression you have is incorrect. If you think about it you'll realize that it is the same as
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|