November 13th, 2002, 08:15 PM
|
#1 (permalink)
| | Member
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.
|
| |
November 14th, 2002, 08:38 PM
|
#2 (permalink)
| | Member
Join Date: Oct 2002 Location: Salem, Oregon
Posts: 480
|
anyone know?  |
| |
November 14th, 2002, 09:15 PM
|
#3 (permalink)
| | Member
Join Date: Oct 2002 Location: Salem, Oregon
Posts: 480
|
no one is answering, so could someone shut down this thread please? |
| |
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;
} |
| |
November 15th, 2002, 12:01 AM
|
#5 (permalink)
| | Member
Join Date: Oct 2002 Location: Salem, Oregon
Posts: 480
|
Thanks for the help  |
| |
November 16th, 2002, 06:30 PM
|
#6 (permalink)
| | Member
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  |
| |
November 17th, 2002, 12:07 AM
|
#7 (permalink)
| | Ultimate Member
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? |
| |
November 17th, 2002, 12:26 AM
|
#8 (permalink)
| | Senior Member
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. |
| |
November 17th, 2002, 02:15 AM
|
#9 (permalink)
| | Junior Member
Join Date: Jun 2002
Posts: 20
|
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. |
| |
November 17th, 2002, 12:43 PM
|
#10 (permalink)
| | Ultimate Member
Join Date: Oct 2002 Location: Scotland, UK
Posts: 3,221
| 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
|
| | |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | | |
Posting Rules
| You may post new threads You may post replies You may not post attachments You may not edit your posts HTML code is Off | | | | Most Active Discussions | | | | | Recent Discussions  | | | | | |