Behaviour with a function I don't understand - help needed

Hi, I have written a basic script (all my scripts are still basic at this stage) for creating a text file on the desktop. This file is needed for another script to reference, so I wanted to automate it’s creation on different computers. The script works fine, but there is a code block that is repeated, so I wanted to make it a function. As soon as I do this it fails, with an error saying Finder can’t continue with the function.

Here is the script:

global desktopPath

set desktopPath to path to desktop

global fileName

set fileName to "Computer.txt"

global filePath

set filePath to (desktopPath & fileName) as string


on makeFile()
	
	display dialog "Which computer is this?" buttons {"Main", "Backup"}
	set buttonResult to button returned of the result
	make new file at desktopPath with properties {name:fileName, type:"text"}
	set newFile to open for access filePath with write permission
	write buttonResult to newFile
	close access newFile
	
end makeFile

tell application "Finder"
	
	-- check if computer file exists
	if file filePath exists then
		display dialog "Computer file already exists, delete and recreate?" buttons {"Yes", "No"} default button 1 cancel button 2
		
		delete file filePath
		
		makeFile()
		
	else
		makeFile()
	end if
end tell

The script checks to see if the file exists, if not, creates the file, if it does it asks if it should recreate the file. Creating the file asks a question, the answer to which is written as the contents of the text file.

If I replace the two points where the function is called with the code block from within the function then it works perfectly. But I get Finder cannot continue with makeFile() when it’s in a function. What am I not doing right here?

thank you
ps. I wasn’t sure how to put in code, so let me know if there is a better way.

I’m a beginner also, but this version seems to work. I added “my” twice before the name of the function, added a tell block to the function, and moved it out of the run block. Someone who knows about AppleScript would do better.

property desktopPath : path to desktop
property fileName : "Computer.txt"
property filePath : (desktopPath & fileName) as string

tell application "Finder"
	-- check if computer file exists
	if file filePath exists then
		display dialog "Computer file already exists, delete and recreate?" buttons {"Yes", "No"} default button 1 cancel button 2
		delete file filePath
		my makeFile()
	else
		my makeFile()
	end if
end tell

on makeFile()
	tell application "Finder"
		display dialog "Which computer is this?" buttons {"Main", "Backup"}
		set buttonResult to button returned of the result
		make new file at desktopPath with properties {name:fileName, type:"text"}
		set newFile to open for access filePath with write permission
		write buttonResult to newFile
		close access newFile
	end tell
end makeFile

To paste in the code, put a line of three backticks, like this ``` then the code, then another line of three backticks.