LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 07-09-2008, 01:38 AM   #1
gaynut
LQ Newbie
 
Registered: Jan 2008
Posts: 27

Rep: Reputation: 15
Question pass arguments and return values from a function in UNIX


hi,

is it possible to pass arguments and get return values from a function in UNIX ,as we do in C , ?

Incase, yes, then how should i write my function?


thanks,
~ gaynut
 
Old 07-09-2008, 02:19 AM   #2
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
What do you mean by "in unix"? Shell scripting?
 
Old 07-09-2008, 04:55 AM   #3
gaynut
LQ Newbie
 
Registered: Jan 2008
Posts: 27

Original Poster
Rep: Reputation: 15
yes, shell scripting.
 
Old 07-09-2008, 05:24 AM   #4
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
Yes you can pass arguments in borne style shells like bash. For example:
Code:
#!/bin/bash

my_func () {
    echo "my_func number of arguments: $#"
    echo "my_func arg 1 is $1"
    echo "my_func arg 2 is $2"
    echo "my_func arg 3 is $3"
    echo "my_func arg 4 is $4"
    echo "my_func arg 5 is $5"
}

my_func hello there bob
The return value is a little different - it must be an integer between 0 and 255, and is assigned to $? when the function returns. For example:
Code:
#!/bin/bash

random_0_to_3 () {
    return $(($RANDOM % 4))
}

for ((i=0; i<5; i++)); do
    random_0_to_3
    echo "return value was $?"
done
In general, the return value is only used to determine success or failure of the function, in the same way you'd use the value of $? after calling a regular shell command.

If you want your function to produce some output which can then be used, you can use stdout, and run the command with the $() syntax. For example:
Code:
#!/bin/bash

quantity_description () {
    if   [ $1 -ge 4 ]; then echo "a lot of beaks to feed"
    elif [ $1 -eq 3 ]; then echo "quite a few beady eyes staring at me"
    elif [ $1 -ge 2 ]; then echo "a pair of them"
    elif [ $1 -eq 1 ]; then echo "just one, all on his lonesome"
    elif [ $1 -eq 0 ]; then echo "none at all"
    fi
}

for ((i=0; i<10; i++)); do
    number=$((RANDOM % 6))
    echo "I have $number penguins, which makes $(quantity_description $number)"
done
 
Old 07-10-2008, 01:56 AM   #5
gaynut
LQ Newbie
 
Registered: Jan 2008
Posts: 27

Original Poster
Rep: Reputation: 15
thank u for the clear explanation.
 
  


Reply

Tags
arguments, from, function, pass, return, unix, values


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
getting function return values in a Bourne script? ocicat Programming 5 08-01-2007 07:01 AM
BASH Script: variable values referencing for console arguments sadarax Programming 1 11-14-2005 05:23 PM
How to pass structure as function arguments ssg14j Programming 2 08-20-2005 09:59 PM
Streaming in values without defined command line arguments liguorir Linux - Software 1 05-07-2004 02:40 AM
How can we pass arguments to installpkg ? frenchi Slackware 3 04-23-2004 06:43 PM


All times are GMT -5. The time now is 03:42 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration