[英]does return statement actually return the value or store it in function?
[英]How does return function return a value to If statement?
值1如何传递到主函数中的if语句,返回函数如何在其中进行递归调用?
#include <stdio.h>
#include <stdlib.h>
int identicalTrees(struct node* a, struct node* b)
{
if (a==NULL && b==NULL)
return 1;
if (a!=NULL && b!=NULL)
{
return
(
a->data == b->data &&
identicalTrees(a->left, b->left) &&
identicalTrees(a->right, b->right)
);
}
return 0;
}
int main()
{
if(identicalTrees(root1, root2))
printf("Both tree are identical.");
else
printf("Trees are not identical.");
getchar();
return 0;
}
调用已声明要返回值的方法时,每次调用该方法时,堆栈上都会为返回值保留空间。 Return语句将值放在堆栈上的该位置,并退出方法,返回到调用该方法的代码。 调用该方法的代码检索Return语句放入堆栈的值,并在If语句中使用它。
作为递归,对方法的每个后续调用都会在堆栈顶部添加其自己的本地堆栈变量空间。 执行某个方法时,当前堆栈指针的顶部将递减以“释放”该方法的堆栈空间。
参见http://en.wikipedia.org/wiki/Stack-standing_programming_language和http://en.wikipedia.org/wiki/Recursion_(computer_science)和https://www.cs.umd.edu/class/fall2002/有关详细说明,请参见cmsc214 / Tutorial / recursion2.html 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.