Menu

How to deal with the return of a function?

2002-12-17
2012-09-26
  • Nobody/Anonymous

    Im sorry, my english writting is horrible, please read the above and you will understand what i am asking you help on:
    int x; //This is declared somewhere, and other functions set its value.

    int function a() { //This is called by mainFunction.
    if x==1 {
    return 0;
    }
    else {
    return 1;
    }
    }

    int masterFunction() {
    a();
    //if a() returns 0 do this
    //else do that
    }

    How do i do what i wrtote in the commented lines of mainFunction?
    Thanks!

     
    • Nobody/Anonymous

      Hi,

      1. Function definition is like this:
      declaration-specifiers function-name (parameter-type-list)
      declaration-list<opt>
      compound-statement

      so no keyword "function".

      2. If statement:
      if (expression) statement

      so expression must be between parentheses.

      3. Return with expression:
      expression is returned to caller as value of function call expression

      so you either write if (a () == 0) do this or assign a() to variable.

      tkorrovi

       
    • Derek Baker

      Derek Baker - 2002-12-17

      int x; //This is declared somewhere, and other functions set its value.

      int a() { //This is called by mainFunction.
      if (x==1) {
      return 0;
      }
      else {
      return 1;
      }
      }

      int masterFunction() {
      if (a() == 0){
      //do this
      }
      else{

      cout << "not zero" << endl;
      }
      }

      Derek

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.