https://dev.mysql.com/doc/refman/8.0/en/innodb-deadlocks-handling.html
When modifying multiple tables within a transaction, or different sets of rows in the same table, do those operations in a consistent order each time. Then transactions form well-defined queues and do not deadlock. For example, organize database operations into functions within your application, or call stored routines, rather than coding multiple similar sequences of INSERT, UPDATE, and DELETE statements in different places.
I couldn't understand why it says to use functions
Using functions isn't strictly necessary, but it helps in many code projects to use fewer, well-reviewed code paths so you can encourage the idea of consistent order of data tasks. If locks are acquired in the same order, they don't deadlock.
If the alternative on a large code project is that every developer codes whatever SQL they want in any order they want, there's a higher risk that they use different order of SQL statements because the developers don't coordinate with other developers.
You can in theory achieve the same thing without using functions, but it requires better communication between members of the developers in the team.