0

I want to know how this function works

//i want to know what happens after the loop ends, does it return false or what // if all is good and inside the loop return true then what happens after the loop ends does the function go to line return false or the function just return true ​bool​ ​vote​(string name) ​{ ​    ​for​(​int​ i = ​0​ ; i < candidate_count; i++) ​    { ​        ​if​(​strcmp​(candidates[i].​name​, name) == ​0​) ​        { ​        candidates[i].​votes​++; ​        ​return​ ​true​; ​        } ​    } ​    ​return​ ​false​; ​}

28th Jun 2022, 1:06 PM
Abdallah Ashraf
6 Answers
+ 1
The loop searches for a candidate whose name matches <name> argument. If it should find one, then it will increment the candidate's <votes> member and function returns true. If none of the candidates names matched <name> argument given, function will return false.
28th Jun 2022, 2:59 PM
Ipang
+ 1
Ipang so this is mean if the condition is true and loop ends it won't go over to the last line that says return false This is what i don't understand
28th Jun 2022, 3:03 PM
Abdallah Ashraf
+ 1
Not quite. Function returns false when the loop runs thorough with no candidate's name found to match (equals to) the given <name> argument. Function returns true when it finds one candidate whose name matches the given <name> argement. When a match was found, the loop cease iteration., and return true immediately after incrementing the respective candidate's vote counter.
28th Jun 2022, 3:12 PM
Ipang
0
The function returns a boolean (determined by the "bool" in front of the function name), it will. But that's only once the function has finished executing, otherwise it would go on forever. Also, this is most likely for CS50 since it's a direct copy of one of the functions we have to complete. If so, they have a discord where you can ask questions directly from other people who are also taking the course as well as Harvard staff to help along.
28th Jun 2022, 1:56 PM
Justice
Justice - avatar
0
Justice yeah for cs50, what i don't know is how it will works, it will return true or false and why If condition is true then will it continue to the end of the function and return false or what That what i don't understand
28th Jun 2022, 2:00 PM
Abdallah Ashraf
0
Abdallah Ashraf You can watch the walkthrough for that problem where Brian will explain what is going on.
28th Jun 2022, 2:01 PM
Justice
Justice - avatar