SET commands are not permitted in function.
You can retrieve the current setting of the DATEFIRST option with the function @@datefirst. Here is an example:
CREATE OR ALTER FUNCTION MyFunkyOne(@a date) RETURNS int AS
BEGIN
RETURN (datepart(dw, @a) + @@datefirst - 1) % 7
END
go
SET DATEFIRST 7
SELECT dbo.MyFunkyOne('2024-02-21')
go
SET DATEFIRST 1
SELECT dbo.MyFunkyOne('2024-02-21')
go
SET DATEFIRST 2
SELECT dbo.MyFunkyOne('2024-02-21')