home hardware prices news articles forums photos user reviews
Go Back   Tech Support Forums - TechIMO.com > PC Hardware and Tech > Webmastering and Programming
Join TechIMO for Free!
Register Blogs FAQ Members List Calendar Search Today's Posts Mark Forums Read
Reply Get bargains at  »  Dealighted.com
 
Thread Tools
Currently Active Users: 2869
Discussions: 188,380, Posts: 2,243,463, Members: 232,608
Old November 13th, 2002, 08:15 PM   Digg it!   #1 (permalink)
Member
 
couch potato's Avatar
 
Join Date: Oct 2002
Location: Salem, Oregon
Posts: 480
Another stupid C question, but be patient with me........

The one thing I'm having trouble with on C is calling a function. I just don't get how to call a function. I have nothing that explains it.

Like for instance, making a basic calculator. I want a funtion that can add, a function that can multiply, one that can subtract, and one that can divide. I got eveything else down, just not how to call functions . also, do you have to declare functions?

anyone know?

er......Let me re-phrase this: how do you call a funtion in C?


Last edited by couch potato : November 14th, 2002 at 01:40 AM.
couch potato is offline   Reply With Quote
Old November 14th, 2002, 08:38 PM     #2 (permalink)
Member
 
couch potato's Avatar
 
Join Date: Oct 2002
Location: Salem, Oregon
Posts: 480
anyone know?

couch potato is offline   Reply With Quote
Old November 14th, 2002, 09:15 PM     #3 (permalink)
Member
 
couch potato's Avatar
 
Join Date: Oct 2002
Location: Salem, Oregon
Posts: 480
no one is answering, so could someone shut down this thread please?

couch potato is offline   Reply With Quote
Old November 14th, 2002, 09:44 PM     #4 (permalink)
Member
 
Join Date: Feb 2002
Posts: 161
No need to shut the thread down!

To call a function in C, you type the name of the function, followed by parentheses. Within the parentheses, you place any parameters which you are passing to the function.

Suppose we have a function caled MyFunction, which takes no parameters and has a return type of void.

void MyFunction(void)
{
return;
}

To call MyFunction, we simply do the following:
MyFunction();

Suppose that we have a function which takes a few parameters. The function might look something like this
void MyNewFunction(int a, double b)
{
return;
}

When we call this function, we need to pass it parameters. We do this by placing the paramaters between the parentheses:

MyNewFunction(5, 99.99999);


In general, functions must be declared before they are used. So you can either do this:

// Declare the function here
void SomeFunc(void);

int main()
{
// Use it here
SomeFunc();
return 0;
}

// Define it here
void SomeFunc(void)
{
return;
}


or this:


// Declare and define function here
void SomeFunc(void)
{
return;
}

int main()
{
// Use it here
SomeFunc();
return 0;
}
Martee is offline   Reply With Quote
Old November 15th, 2002, 12:01 AM     #5 (permalink)
Member
 
couch potato's Avatar
 
Join Date: Oct 2002
Location: Salem, Oregon
Posts: 480
Thanks for the help
couch potato is offline   Reply With Quote
Old November 16th, 2002, 06:30 PM     #6 (permalink)
Member
 
couch potato's Avatar
 
Join Date: Oct 2002
Location: Salem, Oregon
Posts: 480
sorry, im still a little confused. i think i could get it if you could put a small program here that has a couple of basic funtion(s) or one that has just one fuction. so basically, write a small smaple of code using a function. thanks
couch potato is offline   Reply With Quote
Old November 17th, 2002, 12:07 AM     #7 (permalink)
Ultimate Member
 
Nighthawk's Avatar
 
Join Date: Oct 2001
Location: Montreal, QC
Posts: 1,950
I think what you're talking about is more of a user interaction question.. Functions (usually called methods) are something that a program does internally. I am guessing you want your calculator program to start, then get some input from the user, then go do its thing.. I know how to do it in VB and in Java, but not in C.

The way to do this would be to have a void main that starts up, then asks the user to "please enter a function" or something like that. Then the user would type in the function, then two numbers, then the program would write out the result, right?
Nighthawk is offline   Reply With Quote
Old November 17th, 2002, 12:26 AM     #8 (permalink)
Senior Member
 
J-Excel's Avatar
 
Join Date: Aug 2002
Location: Kzoo, MI
Posts: 881
Hey Couch Potato, when I tought myself C I picked up a book at my local library. Can't remember which one I got, but I'm sure there are several out there that would help.
J-Excel is offline   Reply With Quote
Old November 17th, 2002, 02:15 AM     #9 (permalink)
vb6
Junior Member
 
Join Date: Jun 2002
Posts: 20
Send a message via ICQ to vb6
A book would definately help heaps .

Perhaps post some code you have written and i sure everyone will be glad to help you out.

I lost all my source codes. Reformated the wrong drive so i can't help much by providing you a live example.
vb6 is offline   Reply With Quote
Old November 17th, 2002, 12:43 PM     #10 (permalink)
Ultimate Member
 
nukes's Avatar
 
Join Date: Oct 2002
Location: Scotland, UK
Posts: 3,221
Send a message via AIM to nukes Send a message via Yahoo to nukes
Code:
/* Here is a program to show you haw to use functions and pass variables into them, it does not deal with refernecing though. */
#include <stdio.h>
int add_them(a, b)
int main()
  {
    int a, b, c;
    a = 5;
    b = 10;
    c = add_them();
    printf("Result of 5+10 = %d",c);
    return 0;
  }

int add_them(a, b) {
  int a, b, c;
  c = a+b;
  return c; }
How's that? (Probally won't even work )
__________________
_____
NuKeS
nukes is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may post new threads
You may post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Most Active Discussions
Is It Just Me? (2885)
The United States Debt (20)
Looks like Burris will get his Sena.. (8)
I think I just killed my computer w.. (24)
Upgrading RAM (5)
Folderchat Weekday thread (439)
Antec 300 bulk purchase? (11)
Worth the upgrade?? (14)
Help with an Ati Radeon HD 4850 512.. (25)
Recent Discussions
Building a gaming computer advi.. (1)
Best digital camera for under 2.. (14)
Help with an Ati Radeon HD 4850.. (25)
Install Problem for Windows Def.. (0)
New Build ( Finally ) (1)
dual monitors wont boot (0)
Folderchat Weekday thread (439)
MSN Hotmail Down??? (7)
Laptop waking up itself (0)
Blackberry Storm, Gears of War .. (1)
Core 2 Quad Q9550 system (3)
COWBOOM Ripoff! Used Laptop w/$.. (4)


All times are GMT -4. The time now is 08:44 PM.
TechIMO Copyright 2008 All Enthusiast, Inc.



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28