• Skip to main content
  • Skip to footer

SoftwareProg.com

Software programming learning aide

  • Blog
    • Discussion
  • News
  • CS50’s Introduction to Computer Science by Harvard University on Edx
    • Week 1
    • Week 2
    • Week 3
    • Week 4
    • Week 5

Understanding void and return 0/1 with an example

CS50 threads to aide as a supplementary resource › Forums › CS50’s Introduction to Computer Science by Harvard University on Edx › Week 2 › Understanding void and return 0/1 with an example

Tagged: functions, scrabble lab problem, value returned by function, void

  • This topic is empty.
Log In Register Lost Password
Viewing 1 post (of 1 total)
  • Author
    Posts
  • December 5, 2021 at 7:29 am #63
    admin
    Keymaster


      Copy Code
      Copied
      Use a different Browser

      
      
      #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");
          }
      }
      
      

       

      In Lecture 1, along with the above code, it is mentioned:

      “The void before the meow function means that it doesn’t return a value, and likewise in main we can’t do anything with the result of meow, so we just call it.”

      I thought to make meow function return a value and so tried with this code:


      Copy Code
      Copied
      Use a different Browser

      
      
      #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;
      }
      

      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]

    • Author
      Posts
    Log In Register Lost Password
    Viewing 1 post (of 1 total)
    • You must be logged in to reply to this topic.
    Log In

    Log in / Register

    Initiative by Digital Splendid OPC Pvt. Ltd.