Operating System - Linux
1755061 Members
3066 Online
108829 Solutions
New Discussion юеВ

Number and name of parameters given for a function ...

 
SOLVED
Go to solution
Manuales
Super Advisor

Number and name of parameters given for a function ...

Hi ...
i'd like to remember how do you kwno number of elements given in a function?
for example:

we suppose function is named "find_words"

and find_word requires some parameters:

root@user1> find_words lettera letterb letterc letterd

i do not remember how to know the number and parameters given when find_words in invoked,
i think is like $? $#, i do not remember..
before the script work is neccesary verify if all parameters are given and if they are name correct ..
could you help me please?

thanks, Manuales.
4 REPLIES 4
Pete Randall
Outstanding Contributor

Re: Number and name of parameters given for a function ...

$# gives the number of paramaters.


Pete

Pete
Pete Randall
Outstanding Contributor

Re: Number and name of parameters given for a function ...

I should have continued that the individual paramters are identified by $1, $2 and so on.


Pete

Pete
James R. Ferguson
Acclaimed Contributor
Solution

Re: Number and name of parameters given for a function ...

Hi Manuales:

The number of arguments passed to a function is known with '$#'. Unfortunately, in HP-UX the *name* of the function executing doesn't differ from the global name. Consider:

cat /tmp/fct
#!/usr/bin/sh
echo "$0 saw $# arguments"
function myf
{
echo "...but $0 saw $# arguments..."
}
myf what did he see
exit 0

/tmp/fct one two three
/tmp/fct saw 3 arguments
...but /tmp/fct saw 4 arguments...

Regards!

...JRF...
James R. Ferguson
Acclaimed Contributor

Re: Number and name of parameters given for a function ...

Hi (again) Manuales:

Remember, the manpages are your best friend! The ones for 'sh-posix' or 'ksh' provide a tremendous reference:

http://www.docs.hp.com/en/B2355-60103/sh-posix.1.html

http://www.docs.hp.com/en/B2355-60103/ksh.1.html

Regards!

...JRF...