Click here to Skip to main content
16,008,469 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello,

I am practicing C programming. I want to call a function in Main, how to do that? please help.

Detail:

Call Function (Print) from Recurression.cpp to MainC.cpp

I am using following Environment:

Visual Studio C++ 2010 Express ---> Win32 Console Application (Visual C++)
C++
Recurression.cpp
 ------------

#include <stdio.h>
 
void print(int);
 

void print(int n)
{
  static int c = 1;
 
  if (c == n+1)
    return;
 
  printf("%d\n", c);
  c++;
  print(n);
}

MainC.cpp
-------

#include <stdio.h>

int main()
{
  int n;
 
  scanf("%d", &n);
 
  print(n);
 
  return 0;
}

Thanks
Posted
Updated 26-Dec-12 22:43pm
v2

Some minor changes that will help you:
C++
// Recurression.cpp
//  ------------
 
#include <stdio.h>
 
void print(int n)
{
  static int c = 1;
 
  if (c == n+1)
    return;
 
  printf("%d\n", c);
  c++;
  print(n);
}
</stdio.h>


Need to define the print function in module Main.cpp.
C++
// MainC.cpp
// -------
 
#include <stdio.h>
 
void print(int);
 
int main()
{
  int n;
 
  scanf("%d", &n);
 
  print(n);
 
  return 0;
}
</stdio.h>
 
Share this answer
 
Comments
Ajain A K 28-Dec-12 8:19am    
Thanks Richard, good help
Some good news for you: you already call this function in the line print(n); now it's the time to understand what is it. :-)

Please read the elementary C manual. Explaining such elementary things out of the more general context to the one who did not do it is extremely difficult, almost impossible. Makes little sense anyway. Read it first.

—SA
 
Share this answer
 
v2
Comments
Ajain A K 28-Dec-12 8:20am    
very nice advice, thanks for document reference
Sergey Alexandrovich Kryukov 28-Dec-12 12:36pm    
You are welcome.
Will you formally accept this answer, too?
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900