Engineer's Asylum

Implicit return at end of non-void function ( warning )

I want to convert data in an array into tokens. The code works fine with a warning “implicit return at end of non-void function”. I am writing this code in XC8 compiler for pic18f4550.

char* Server() {                            
    int i = 0; 
    int counter = 0;
    char data[512];               
    static char *t;
    t = strtok(data, "-");               

    while(t != NULL) {  
         if(counter == 1) {
              return t;
         }
         t = strtok(NULL,"-");  
         counter++;
    } 
}

What if the if condition in the while loop is not true? You know that it’s not going to be false. but the compiler doesn’t. It’s a bad practice to use multiple returns inside a function.

Seems as if you should have a return in case your if condition != 1, because if counter never reaches 1 (t == null ), it will go to the end of the function and return nothing (Which can cause all sorts of problems).

1 Like

As Cisco said, it can cause all sorts of problem. All you have to do is add a return NULL statement just after the closing of the while loop.

char* Server() {                            
    int i = 0; 
    int counter = 0;
    char data[512];               
    static char *t;
    t = strtok(data, "-");               

    while(t != NULL) {  
         if(counter == 1) {
              return t;
         }
         t = strtok(NULL,"-");  
         counter++;
     } 
     return NULL; 
}