function-definition is not allowed here before '{' token ERROR

Remember two things:
The only thing allowed outside of a function is a variable or function declaration.
Functions cannot be declared inside a function.