Page 179 - C-Language
P. 179

}


        However you can also use a struct as a return value which allows you to return both an error
        status along with other values as well. For instance.


         typedef struct {
             int    iStat;      /* Return status */
             int    iValue;     /* Return value */
         }  RetValue;

         RetValue func (int iValue)
         {
             RetValue iRetStatus = {0, iValue};

             if (iValue > 10) {
                 iRetStatus.iValue = iValue * 45;
                 iRetStatus.iStat = 1;
             }

             return iRetStatus;
         }


        This function could then be used like the following sample.


         int usingFunc (int iValue)
         {
             RetValue iRet = func (iValue);

             if (iRet.iStat == 1) {
                 /* do things with iRet.iValue, the returned value */
             }
             return 0;
         }


        Or it could be used like the following.


         int usingFunc (int iValue)
         {
             RetValue iRet;

             if ( (iRet = func (iValue)).iStat == 1 ) {
                 /* do things with iRet.iValue, the returned value */
             }
             return 0;
         }


        Read Function Parameters online: https://riptutorial.com/c/topic/1006/function-parameters

















        https://riptutorial.com/                                                                             155
   174   175   176   177   178   179   180   181   182   183   184