用C递归函数:返回是总是需要? [英] recursive functions in C: is return always necessary?

查看:119
本文介绍了用C递归函数:返回是总是需要?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我第一次用递归函数玩,而这个功能,我写返回一个字符串的大小,如果它包含按升序只有字母,如果没有返回-1。

我不明白为什么它的工作原理为codeS后,我拿出了第二个回归。一个比另一个更浪费了?将AP preciate一些见解。

收益 only_ascending_letters(字符串,指数+ 1);

 的#include<&stdio.h中GT;    INT only_ascending_letters(字符字符串[],INT指数);    无效的主要(){
        字符字符串1 [] =我的名字叫皮特
        字符字符串2 [] =aabcdefg;        的printf(第一个字符串为%d,第二个为%d \\ n,only_ascending_letters(string1,0),only_ascending_letters(string2,0));    }    INT only_ascending_letters(字符字符串[],INT指数){
        如果回报指数(字符串[指数]!);
        if(((string[index]>='a'&&string[index]<='z')||(string[index]>='A'&&string[index]<='Z'))&&((string[index]<=string[index+1])||!string[index+1]))
            返回only_ascending_letters(字符串,索引+ 1);
        否则返回-1;    }

用only_ascending_letters(字符串,索引+ 1);

 的#include&LT;&stdio.h中GT;    INT only_ascending_letters(字符字符串[],INT指数);    无效的主要(){
        字符字符串1 [] =我的名字叫皮特
        字符字符串2 [] =aabcdefg;        的printf(第一个字符串为%d,第二个为%d \\ n,only_ascending_letters(string1,0),only_ascending_letters(string2,0));    }    INT only_ascending_letters(字符字符串[],INT指数){
        如果回报指数(字符串[指数]!);
        if(((string[index]>='a'&&string[index]<='z')||(string[index]>='A'&&string[index]<='Z'))&&((string[index]<=string[index+1])||!string[index+1]))
        / *掏出返回* / only_ascending_letters(字符串,索引+ 1);
        否则返回-1;    }


解决方案

是的,你绝对的需求的回报。需要注意的是C语言的规则是一个有点松懈了这个问题,如果你没有使用的返回值,这将是没有它的罚款。但是,您使用的返回值,所以你需要return语句。

您看到的可能是由于实施细节功能上的某些架构通过一个众所周知的寄存器设置为该值(在i386上EAX)返回(积分值)引起的。因此,如果最下面的递归调用确实收益,并在两者之间在该寄存器中没有蹬地,你看到它那种工作设置该寄存器和电话。但是,你不能依靠这一点。

请注意,良好的编译器会认识到这是一个尾递归调用编译变种都基本上是一样的。

this is my first time playing with recursive functions, and this function that I wrote returns the size of a string if it contains only letters in ascending order, and if not it returns -1.

I don't understand why it works for both codes, after I took out the second "return". Is one more wasteful than the other? Would appreciate some insight.

with "return only_ascending_letters(string, index+1);"

 #include <stdio.h>

    int only_ascending_letters(char string[], int index);

    void main() {
        char string1[]="Hi my name is pete";
        char string2[]="aabcdefg";

        printf("the first string is %d and the second one is %d\n",only_ascending_letters(string1,0),only_ascending_letters(string2,0));

    }

    int only_ascending_letters(char string[], int index){
        if(!string[index]) return index;
        if(((string[index]>='a'&&string[index]<='z')||(string[index]>='A'&&string[index]<='Z'))&&((string[index]<=string[index+1])||!string[index+1])) 
            return only_ascending_letters(string, index+1);
        else return -1;

    }

with "only_ascending_letters(string, index+1);"

 #include <stdio.h>

    int only_ascending_letters(char string[], int index);

    void main() {
        char string1[]="Hi my name is pete";
        char string2[]="aabcdefg";

        printf("the first string is %d and the second one is %d\n",only_ascending_letters(string1,0),only_ascending_letters(string2,0));

    }

    int only_ascending_letters(char string[], int index){
        if(!string[index]) return index;
        if(((string[index]>='a'&&string[index]<='z')||(string[index]>='A'&&string[index]<='Z'))&&((string[index]<=string[index+1])||!string[index+1])) 
        /*Took out the return*/ only_ascending_letters(string, index+1);
        else return -1;

    }

解决方案

Yes, you absolutely need the return. Note that C language rules are a bit lax about this issue, and if you hadn't used the return value, it would be fine without it. However, you use the return value, so you need the return statement.

What you see is probably caused by the implementation detail that function on some architectures return (integral values) by setting a well known register to that value (eax on i386). Therefore, if the bottommost recursive call does return and set this register, and the calls in-between don't stomp on that register, you see that it sort of works. However, you mustn't rely on that.

Note that good compilers will recognize this is a tail-recursive call and compile both variant basically the same way.

这篇关于用C递归函数:返回是总是需要?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆