1、Hello World
::调用函数
call :func
goto :eof
::函数
:func
echo hello world
goto:eof
2、带参数的函数
::调用函数
call :func 5 , 10
goto :eof
::函数
:func
echo the value of parameter 1 is %~1
echo the value of parameter 2 is %~2
goto :eof
3-1、带返回值 、首字母大写的函数
:: 调用函数
set "button=repeatbutton"
set "components=!button!"
call :first_letter_cap components
:: 显示返回值
echo !components!
goto :eof
:: 函数
:first_letter_cap
set letters=A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
set "str=!%~1!"
echo !str!
set "str1=!str:~1!"
echo !str1!
set "str2=!str:~,1!"
echo !str2!
:: /i:表示不分大小写
for %%i in (%letters%) do if /i "%%i"=="!str2!" set str2=%%i
set "%~1=!str2!!str1!"
goto :eof
3-2、带返回值 、首字母大写的函数
:: 调用函数
set "button=repeatbutton"
set "components=!button!"
call :first_letter_cap components, return
:: 显示返回值
echo !return!
goto :eof
::函数
:first_letter_cap
set letters=A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
set "str=!%~1!"
set "str1=!str:~1!"
set "str2=!str:~,1!"
for %%i in (%letters%) do if /i "%%i"=="!str2!" set str2=%%i
set "return=!str2!!str1!"
goto :eof
函数的参数、返回值就像全局变量, 参数可当返回值使用,
4、代码上下文
@echo off
setlocal enabledelayedexpansion
...code...
endlocal
pause