#1.1. define a function that prints information about the program and arguments
func1() for w ; do echo "$w"; done
func2() { for w ; do echo "$w"; done; echo $$; return 255; }
##print the func definition
declare -f func1
declare -f func2
#2.2. call the function and print the return value
echo $$
##function will be called in the same shell with a return code
func2 param{1..3} #or func param1 param2 param3
echo "the return value is $?"
echo $$
-------output----------
#1.1. define a function that prints information about the program and arguments
##print the func definition
func1 ()
{
for w in "$@";
do
echo "$w";
done
}
func2 ()
{
for w in "$@";
do
echo "$w";
done;
echo $$;
return 255
}
#2.2. call the function and print the return value
18824
##function will be called in the same shell with a return code
param1
param2
param3
18824
the return value is 255
18824
|
#1.1. define a function that prints information about the program and arguments
sub func{\
print "\$0 returns the program name: $0\n";\
print "\$_[0] returns the 1st argument: $_[0]\n";\
print "\@_ returns the argument list: @_\n";\
print "\$\$ returns the shell PID: $$\n";\
print "\$#_ returns the argument number : 1+$#_\n";\
return scalar(@_);}
# or func (param1,param2,param3);
$ret=func (param1..param3);
print "the return value is $ret";
-------output----------
#1.1. define a function that prints information about the program and arguments
# or func (param1,param2,param3);
$0 returns the program name: -e
$_[0] returns the 1st argument: param1
@_ returns the argument list: param1 param2 param3
$$ returns the shell PID: 18825
$#_ returns the argument number : 1+2
the return value is 3
|
# function, no positional parameters?
def func(p1,p2):
print(p1, p2)
func("param1","param2")
# or
tup=("param1","param2"); func(*tup)
# use tuple as parameter
def func2(*prms):
print(prms) #*prms is tuple
func2("one",2,3.0)
tup=("one",2,3.0); func2(*tup)
# use dict as parameter
def func3(**ages):
print(ages) #**ages is dictionary
func3(dad=42,mom=48,lisa=7)
# or
dic={'dad':42,'mom':48,'lisa':7}; func3(**dic)
# parameters with default value
def func4(p1='tst1',p2='tst2'):
print(p1+" "+p2)
func4()
-------output----------
# function, no positional parameters?
('param1', 'param2')
# or
('param1', 'param2')
# use tuple as parameter
('one', 2, 3.0)
('one', 2, 3.0)
# use dict as parameter
{'dad': 42, 'lisa': 7, 'mom': 48}
# or
{'dad': 42, 'lisa': 7, 'mom': 48}
# parameters with default value
tst1 tst2
|