简体   繁体   中英

Recursive factorial error return-statement with a value, in function returning 'void' [-fpermissive]

I try to write a factorial using recursive function. What is wrong with this code? I get error return-statement with a value, in function returning 'void' [-fpermissive]

#include <iostream.h>

int factorial(int);

int factorial(int number)
{
    return number==0?1: number * factorial*(number - 1);
}

void main(void)
{
    int number;

    cout << "Please enter a natural number: ";
    cin >> number;
    if (number < 1)
        cout << "That is not a natural number.\n";
    else
        cout << number << " factorial is: " << factorial(number) << endl;
    return 0;
}

The function main should return int , not void . Also void within parenthesis is useless in C++.

Use:

int main() {

or:

auto main() -> int {

fix the code. main must be int and function argument should be entered immediately without * in between:

#include <iostream.h>

int factorial(int number)
{
    return number==0?1: number * factorial(number - 1);
}

int main(void)
{
    int number;

    cout << "Please enter a natural number: ";
    cin >> number;
    if (number < 1)
        cout << "That is not a natural number.\n";
    else
        cout << number << " factorial is: " << factorial(number) << endl;
    return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM