简体   繁体   English

C中的返回值函数

[英]Return Value Function in C

Hello I'm working on a quick program that generates a master code and the user has to guess the code. 您好我正在开发一个生成主代码的快速程序,用户必须猜测代码。 When checking the user's guess I use 2 functions one for Exact Matches and the other for close matches (where they got a number that's in the mastercode but not in the right location) 当检查用户的猜测时,我使用2个函数用于精确匹配,而另一个用于紧密匹配(其中他们得到的数字在主代码中但不在正确的位置)

Ex. 防爆。 master code 1 2 3 4 主码1 2 3 4

user 2 3 2 4 the output the should show the user has 2 close matches and one exact match. 用户2 3 2 4输出应显示用户有2个近似匹配和一个完全匹配。 I'm having trouble understanding how to properly return an int. 我无法理解如何正确返回int。

My ouput just shows the default value of exactMatch and closeMatch when I try to print them in Main. 当我尝试在Main中打印时,我的输出只显示了exactMatch和closeMatch的默认值。 any insight will be greatly appreciated. 任何见解将不胜感激。 Thank you. 谢谢。

 #include<stdio.h>
 #include<math.h>
 #include<stdlib.h>
 #include<time.h>
 #define CODELENGTH 4
 #define NUMSYMBOLS 6


 int MasterCode[4];
 int guess[ 4 ];
 int exactMatch;
 int closeMatch=0;

 void genCode (int MasterCode[])
{
int i=0;
int k;
while (i < CODELENGTH){

MasterCode[i] =rand() %NUMSYMBOLS +1;
    i++;

}//end while loop.
for ( k = 0 ; k < 4; k++ ) {
    printf( "%d ", MasterCode[ k ] );
}

printf( "\n" );
}





 void getGuess (int guess[])
{

int number = 0;

printf( "Please enter your list of 4 numbers between 1 and 6: " );
int j;
int k;
for ( j = 0 ; j < 4; j++ ) {
    scanf( "%d", &number );
    guess[ j ] = number;
}

printf( "Your array has these values: " );

for ( k = 0 ; k < 4; k++ ) {
    printf( "%d ", guess[ k ] );
}

printf( "\n" );
 }




 int main (int argc, char **argv)
 {
srand ( time(NULL) );

genCode(MasterCode);
getGuess(guess);
checkExactMatches(MasterCode, guess, exactMatch);
checkCloseMatches(MasterCode, guess, closeMatch);
printf("%d = Ending exactMatches \n", exactMatch);
printf("%d  = Ending closeMatches \n", closeMatch);





 }
 int checkExactMatches (int MasterCode[], int guess[], int exactMatch )
  {
int woot;
for(woot=0; woot<4; woot++){


        if (MasterCode[woot] == guess[woot]){
            printf("Exact Match found \n");
            exactMatch ++;
            printf( "%d = Guess \n" , guess[ woot ]);
            printf( "%d = MasterCode \n", MasterCode[ woot ]);
            printf("%d = exactMatch \n", exactMatch);

        }// end if

        if (MasterCode[woot] != guess[woot])
            printf("No EXACT match \n");


}//end for loop

return exactMatch;
 } // end checkExactMatches



 int checkCloseMatches (int MasterCode[], int guess[], int closeMatch )
  {
int k;
int j;
for(k=0; k<4; k++){

    for (j=0; j<4; j++) {


        if (MasterCode[k] == guess[j]){
    printf("CLOSE Match found \n");
    closeMatch ++;
    printf( "%d = Guess \n" , guess[ j ]);
    printf( "%d = MasterCode \n \n", MasterCode[ k ]);
    printf("%d = closeMatch \n \n", closeMatch);

}// end if

if (MasterCode[k] != guess[j])
    printf("No CLOSE match \n");


    }//end nested for loop
}//end for loop

return closeMatch;
 } // end checkCloseMatches

Getting the value returned from a function is actually really easy, it's basically the same as assigning a value to a variable. 获取函数返回的值实际上非常简单,它与为变量赋值基本相同。 In your case, the syntax would be something like this: 在您的情况下,语法将是这样的:

int result = checkExactMatches(MasterCode, guess, exactMatch);

result will now hold the value returned by the function. result现在将保存函数返回的值。

What you need to do is 你需要做的是

  • Not pass the count arguments to the function and 不将count参数传递给函数和
  • Instead collect the return value of the function in the count 而是在计数中收集函数的返回值

So 所以

checkExactMatches(MasterCode, guess, exactMatch);

becomes

exactMatch = checkExactMatches(MasterCode, guess);

You need to make appropriate changes to the function header and also avoid using global variables. 您需要对函数头进行适当的更改,并避免使用全局变量。

Note that while arrays are passed by pointer (meaning that the function can modify the pointed-to pointer), ints are passed by value. 请注意,虽然数组是通过指针传递的(意味着函数可以修改指向指针),但是int会按值传递。 Therefore, changes to the int within a function only persist for the duration of the function. 因此,函数中int的更改仅在函数持续时间内持续存在。

You want to use 你想用

exactMatch = checkExactMatches(MasterCode, guess);

and in that case you won't have to pass in exactMatch any more. 在这种情况下,您将不再需要传递exactMatch

C passes arguments by value, rather than by reference, so passing int exactmatch into the function creates a duplicate of the data inside exactmatch , so operations done inside the method don't do anything. C通过值而不是引用传递参数,因此将int exactmatch传递给函数会在exactmatch创建数据的exactmatch ,因此在方法内部执行的操作不会执行任何操作。

What you want to do is remove exactmatch from the argument list, and instead assign it as zero ( int exactmatch=0; at the beginning of the method, remove exactmatch from the global variables, and use something like int resultExact = checkExactMatches(MasterCode, guess); as your function call. You're already returning the answer correctly, you just don't pick up that answer anywhere. 你想要做的是从参数列表中删除exactmatch,而是将其指定为零( int exactmatch=0;在方法的开头,从全局变量中删除exactmatch,并使用类似int resultExact = checkExactMatches(MasterCode, guess);作为你的函数调用。你已经正确地回答了答案,你只是不在任何地方找到答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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