全局与局部变量及递归

来源:互联网 发布:serpentza黑中国知乎 编辑:程序博客网 时间:2024/06/07 19:46

//全局变量, 整个文件中,所有的函数都能使用的变量。
int b;

void function(){
printf(“\nb is %d\n”, b);
b = 478457845;

// 10! 10 * 9 * 8 * …..*1
// n! n * (n-1)(n- 2) (n - 3)…..*1
}

int functionGui(int a){
// 乘法递归
if (a == 1 || a == 0) {
return 1;
}else{
return a * functionGui(a - 1);
}
}
//return的注意事项, return后面的代码不会执行
void func(){
printf(“\n1111111”);
printf(“\n2222222”);
printf(“\n33333333”);
printf(“\n44444444”);
return;
printf(“\n55555555”);
printf(“\n66666666”);
}

//在函数体内部 声明的变量,称为局部变量。 其他函数不能使用
int main(int argc, const char * argv[]) {
@autoreleasepool {
// insert code here…
NSLog(@”Hello, World!”);
int a = 10;
b = 1213;
printf(“a is %d”, a);
function();
printf(” b sasfds is %d,\n”, b);
printf(“%d”, functionGui(10));
func();
}

0 0
原创粉丝点击