IT, Programming, & Web Development › Forums › CS50’s Introduction to Computer Science by Harvard University on Edx › Week 2: [Arrays] – Functions, Variable and Scope, Debugging, Arrays, and Command Line Arguments › Understanding void and return 0/1 with an example
- This topic is empty.
-
AuthorPosts
-
December 5, 2021 at 7:29 am #63
[dm_code_snippet background=”yes” background-mobile=”no” slim=”yes” line-numbers=”yes” bg-color=”#abb8c3″ theme=”dark” language=”clike” wrapped=”yes” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]
#include <stdio.h> void meow(int n); int main(void) { meow(3); } void meow(int n) { for (int i = 0; i < n; i++) { printf("meow\n"); } }
[/dm_code_snippet]
In Lecture 1, along with the above code, it is mentioned:
“The
void
before themeow
function means that it doesn’t return a value, and likewise inmain
we can’t do anything with the result ofmeow
, so we just call it.”I thought to make meow function return a value and so tried with this code:
[dm_code_snippet background=”no” background-mobile=”yes” slim=”yes” line-numbers=”yes” bg-color=”#abb8c3″ theme=”dark” language=”clike” wrapped=”yes” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]
#include <stdio.h> int meow(int n); int main(void) { meow(3); } int meow(int n) { for (int i = 0; i < n; i++) { printf("meow\n"); } return 1; }
[/dm_code_snippet]
This compiles successfully. I need to confirm if return 1 in the above code means that the program runs successfully. Or what is the utility of return 1? Can any function be made int type with bringing in return 0/return 1? Return 0 leads to non compiling in the above instance. My hunch (which actually perhaps not true) is suppose I make any error within the function meow and return 0 should lead to successful compilation as return 0 ensures that the program was unsuccessful in running.
Reply
https://edstem.org/us/courses/176/discussion/909123?answer=2070762[learn_press_profile]
-
AuthorPosts
- You must be logged in to reply to this topic.