|
|||||||
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
#1 |
|
Barefoot on the Moon!
Staff
Premium Member
Join Date: Aug 2002
Location: Northeastern USA
Posts: 13,385
|
Can you call a function within a function in C?
For example: Code:
#include < stdio.h > /*ignore spaces here*/
void call1 (int value1);
void call2 (int value2);
int main (void);
{
call1(value1);
return 0;
}
void call1 (int value1)
{
printf("%d", value1);
call2 (value 2);
return;
}
void call2 (int value2)
{
printf("%d", value2);
return;
}
__________________
There are two secrets to staying young, being happy, and achieving success. You have to laugh and find humor every day, and you have to have a dream.
|
|
|
|
|
|
#2 |
|
Member (13 bit)
Join Date: Jul 2000
Location: Fullerton, CA
Posts: 7,030
|
Yes you can functions within functions. However, the example you wrote won't run because "call1" calls "call2" with the parameter "value2" which doesn't exist within the scope of the the function.
|
|
|
|
|
|
#3 |
|
Barefoot on the Moon!
Staff
Premium Member
Join Date: Aug 2002
Location: Northeastern USA
Posts: 13,385
|
would this run?
Code:
#include < stdio.h > /*ignore spaces here*/
void call1 (int value1);
void call2 (int value2);
int main (void);
{
call1(value1);
return 0;
}
void call1 (int value1)
{
int value2;
printf("%d", value1);
call2 (value 2);
return;
}
void call2 (int value2)
{
printf("%d", value2);
return;
}
|
|
|
|
|
|
#4 |
|
Professional gadfly
|
No, because you don't declare the variable value1 in function main before you pass it to call1.
Also, you don't initialize any variables before you use them. Depending on the compiler you use, it may complain or it may not. In any case, it is good practice to intialize variables to some value, such as 0. |
|
|
|
|
|
#5 |
|
Barefoot on the Moon!
Staff
Premium Member
Join Date: Aug 2002
Location: Northeastern USA
Posts: 13,385
|
how's that?
Code:
#include < stdio.h > /*ignore spaces here*/
void call1 (int value1);
void call2 (int value2);
int main (void);
{
int value 1;
value1=0;
call1(value1);
return 0;
}
void call1 (int value1)
{
int value2;
value2=0;
printf("%d", value1);
call2 (value 2);
return;
}
void call2 (int value2)
{
printf("%d", value2);
return;
}
|
|
|
|
|
|
#6 |
|
Professional gadfly
|
That looks like it will work (as long as you eliminate the spaces in "value 1" and "value 2", but I am sure that you have already done that.
What I meant when I said that was that you did this: int value2; call2(value2); You are declaring value2 to be an int, but you don't set it to anything. Before you assign it a value, you use it in a function call. Not a good idea, because at that point value2 could be any arbitrary number. Some compilers will "fix" this by defaulting to 0, assuming that is what you want, but it is better to be explicit in code and not rely on the compiler. Last edited by doctorgonzo; 10-15-2003 at 11:52 AM. |
|
|
|
|
|
#7 |
|
Barefoot on the Moon!
Staff
Premium Member
Join Date: Aug 2002
Location: Northeastern USA
Posts: 13,385
|
Okay, I think I get it now. Thanks, doctorgonzo
|
|
|
|
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|