Links to docs (like in the accepted answer) are good but good example is better :)
define my_func
$(eval $@_PROTOCOL = "https:")
$(eval $@_HOSTNAME = $(1))
$(eval $@_PORT = $(2))
echo "${$@_PROTOCOL}//${$@_HOSTNAME}:${$@_PORT}/"
endef
my-target:
@$(call my_func,"example.com",8000)
Take into consideration the following:
- There are no custom "functions" in Makefile language. So "my_func" is actually a variable that simply contains a text.
- That "function" doesn't have its own scope. All the content of that "function" is copied into the recipe as is. All variables in the body will be used after that as a part of the recipe.
- Don't use spaces near the commas to prettify param list of "call" function.
- Args are passed like $(1), $(2), ... that is not very handy, so re-declare them with meaningful names. This is optional but recommended for bigger "function" bodies.
- Variables are declared with
$@_
prefix that makes them "local" to the rule (actually not local but prefixed by the target name).
So we have imitation of functions with imitation of local variables but generally this works good.