My guess is that you are passing the wrong class of variable. I would do something like this. I would tell myself that I will always pass strings and then let the code convert the variable to the proper class. I would do this because it's difficult to pass the proper class for things like the command/option/control type keys to System Events. They have their own class that only System Events understands. I don't know how you would pass them otherwise.
So using all strings, a simple if statement can handle the rest. Notice also that I added an application variable too. When you issue keystrokes the keystrokes are issued to the frontmost application, so it's a good practice to ensure the application you want to target is frontmost by "activating" it before performing the keystrokes.
Here's the basic applescript code...
on code(appName, theNum, theModifier)
tell application appName to activate
delay 0.2
set theNum to theNum as number
tell application "System Events"
if theModifier is "command" then
key code theNum using command down
else if theModifier is "option" then
key code theNum using option down
else if theModifier is "control" then
key code theNum using control down
else if theModifier is "shift" then
key code theNum using shift down
end if
end tell
end code
Then you could run it with something like this (notice I'm passing all strings)...
code("Safari", "123", "command")