Gow.
Gow.

Reputation: 55

C recursive function calling concept

/*Program to print all the numbers between a lower bound and a upper bound values*/     

#include<stdio.h>
#include<stdlib.h>
void recur(int a, int b);
int main(void)
{
    int x,y;
    printf("Enter the lower and upper bound values: \n");
    scanf("%d %d",&x,&y);
    void recur(x,y);
    return 0;
}
void recur(int a,int b)
{
    if(a<b)
    {
        printf("%d /n",a);
        a++;
        void recur(a,b);
    }
}

The output I get is:

Enter the lower and upper bound values:
    10
    50
    process returned 0.

Is there anything wrong with the syntax or return type..? I've just started learning c.Need Help

Upvotes: 0

Views: 123

Answers (4)

Anuj Garg
Anuj Garg

Reputation: 582

Here is your working code.

#include<stdio.h>
#include<stdlib.h>
void recur(int a, int b);
int main(void)
{
    int x,y;
    printf("Enter the lower and upper bound values: \n");
    scanf("%d %d",&x,&y);
    recur(x,y);           // change made here
    return 0;
}
void recur(int a,int b)
{
    if(a<b)
    {
        printf("%d \n",a);
        a++;
        recur(a,b);          // change made here
    }
}

Upvotes: -1

Mihai Hangiu
Mihai Hangiu

Reputation: 598

You have the following errors in your program:

  1. When you call a function the syntax is
funcName(param1, param2);

or, if function returns a value:

ret = funcName2(param3, param4, param5);

In your code, putting void at call time is a syntax error:

void recur(a,b);

This is how function is declared, not called. Attention there is a difference between function declaration and function definition.

  1. If you want to print special chars in C, like newline, you need to print '\n' line this:
printf("some message followed by newline \n");

Note that '\n' is a single char, even if you see a '\' and a 'n'. '\' has the purpose to escape the next char 'n' making it a new char. So '\n' is the newline character.

Other special characters in C are: '\t' for tab, '\' to actually print a '\'. Strings in C are enclosed in double quotes like "message" while chars are enclosed in single quotes like 'a', 'b', '\n', 'n', '\'.

Upvotes: 0

Sohil Omer
Sohil Omer

Reputation: 1181

void recur(int a,int b)
{
    if(a<b)
    {
        printf("%d /n",a);
        a++;
        //void recur(a,b); You need to call the function not to declare 
        // there is difference between dec and calling.
       recur(a,b); // is the correct way to do this .
    }
}

same requires in main() method though

Upvotes: 0

Spikatrix
Spikatrix

Reputation: 20244

Both

void recur(x,y);
void recur(a,b);

declares the functions (prototype). To call them, change them to

recur(x,y);
recur(a,b);

Upvotes: 2

Related Questions