-2

I am working on an assignment in C and am being asked to create a function that returns a status word, but I have no clue what that means and even less how to declare the return type and how the returned status word is determined. Please help.

shauryachats
  • 9,975
  • 4
  • 35
  • 48
Morg Man
  • 25
  • 1
  • 4

2 Answers2

1

Сomplementing volerag's post can add such a way:

typedef enum {
    FAIL,
    SUCCESS
} STATUS;

STATUS getStatus() {
    // ...
    return ...;
}

But this doesn't make big difference.

shauryachats
  • 9,975
  • 4
  • 35
  • 48
Cargo Tan
  • 43
  • 3
0

A status word could be a variable (having a value) which tells about the execution of a function - whether it succeeded in doing something it was supposed to do or not.

For example:

int doSomething()
{
    if (somethingCanBeDone)
    return SUCCESS;
    else 
    return FAILURE;
}

Here, SUCCESS and FAILURE are the status words returned from the function which tell whether the function has succeeded in the job it had to do.

You could do it in two ways.

  1. Use a #define statement to define the status words.

    #define SUCCESS 1
    #define FAILURE 0
    
    int doSomething()
    {
        if (somethingCanBeDone)
        return SUCCESS;
        else 
        return FAILURE;
    }
    
    int main()
    {
          int returnval = doSomething();
          if (returnval == SUCCESS) 
          {
                //Do something
          }
          else
          {
                //Do something else.
          } 
    }
    
  2. Use const int statements to define the status words.

    const int SUCCESS = 1;
    const int FAILURE = 0;
    

These are similar to macros, but since they tell about the status of a function, they are known as status words.

shauryachats
  • 9,975
  • 4
  • 35
  • 48