Go Back   PCMech Forums > Help & Discussion > Web Design / Development

Need Some Help? Type Your Keywords Here:

Reply
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
Old 10-13-2003, 11:21 PM   #1
Barefoot on the Moon!
Staff
Premium Member
 
Force Flow's Avatar
 
Join Date: Aug 2002
Location: Northeastern USA
Posts: 13,385
Question C function call

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.
Force Flow is offline   Reply With Quote
Old 10-14-2003, 02:31 AM   #2
Member (13 bit)
 
DrZaius's Avatar
 
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.
DrZaius is offline   Reply With Quote
Old 10-14-2003, 03:42 PM   #3
Barefoot on the Moon!
Staff
Premium Member
 
Force Flow's Avatar
 
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;
  }
Force Flow is offline   Reply With Quote
Old 10-14-2003, 03:57 PM   #4
Professional gadfly
 
doctorgonzo's Avatar
 
Join Date: Jan 2002
Location: Minneapolis, MN
Posts: 6,364
Send a message via MSN to doctorgonzo
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.
doctorgonzo is offline   Reply With Quote
Old 10-15-2003, 11:32 AM   #5
Barefoot on the Moon!
Staff
Premium Member
 
Force Flow's Avatar
 
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;
  }
What do you mean when you say " don't initialize any variables before you use them"?
Force Flow is offline   Reply With Quote
Old 10-15-2003, 11:37 AM   #6
Professional gadfly
 
doctorgonzo's Avatar
 
Join Date: Jan 2002
Location: Minneapolis, MN
Posts: 6,364
Send a message via MSN to doctorgonzo
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.
doctorgonzo is offline   Reply With Quote
Old 10-15-2003, 11:49 AM   #7
Barefoot on the Moon!
Staff
Premium Member
 
Force Flow's Avatar
 
Join Date: Aug 2002
Location: Northeastern USA
Posts: 13,385
Okay, I think I get it now. Thanks, doctorgonzo
Force Flow is offline   Reply With Quote
Reply

Bookmarks

Still Need Help? Type Your Keywords Here:


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -5. The time now is 08:07 AM.
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.
SEO by vBSEO 3.6.0 PL2