提问者:小点点

如何将void函数的“return”写入伪代码[closed]


用伪代码怎么可能做到这一点呢?

想象一下这个例子:

void f(int n)
{
    // some instructions
    if(/* some condition met */)
        return;

    // or continue executing
}

我认为在这种情况下不应该使用终端子算法。

是否有一个特定的语法来代替(我认为它可能是)exit?

我应该如何使用它?


共1个答案

匿名用户

与其用伪代码解释概念,不如理解这一点:

#include <iostream>

// function prototype declaration
void count();

int main(void) {
    // calling a recursive function
    count();

    std::cout << std::endl;

    return 0;
}

void count() {
    // declaring the variable as static to keep the value saved
    // even after the end of the function
    static int counter;

    if (counter == 5)
        // using the 'return' statement to get out of the function
        return;

    // until count < 5, goes on
    counter++;
    std::cout << counter << ' ';

    // recursion
    count();
}

如果函数返回类型是void,那么仍然不需要担心这个问题。 只要在需要退出函数时使用它就可以了。