la funzione e' corretta ed e' fattibile, se nn funziona forse il tuo problema non e' li'....