Just check if the variable is set, using the code from your link:
add_action( 'init', 'set_agent_cookie' );
function set_agent_cookie() {
if (isset($_GET['code'])) {
$name="agent";
$id = $_GET['code'];
setcookie( $name, $id, time() + 3600, "https://wordpress.stackexchange.com/", COOKIE_DOMAIN );
}
}