OK, some "theory of programming 101" seems to be in order:
Subroutines in general come in two forms: procedures and functions. PASCAL had this difference laid down in keywords, while C and all its children blurred this fact by only having functions.
The difference is: functions have one (exactly one!) return value, procedures have none. So one could treat procedures as special functions with a return value of <void> (this in fact is what C does).
In "normal" programming languages the return value of a function could be anything: a number, a string or any other data type the language is capable of to define. For instance:
Will pass the number (constant) 5 as argument to the function "calc_natural_log" and the function will return something (perhaps a floating point number), which in turn gets stored in the variable "mylog".
In shell programming function can only have one sort of return values: an unsigned short short integer, equivalent to the "return code" (or "error level") the OS knows. In fact this is (treated as) the same as this shows:
The if-keyword treats the return code of the function the same way it would treat the RC of any external command:
That leaves the question: how to manipulate data across functions/procedures? First we need to differentiate between data the function only needs to know and data the function needs to change.
Data the function needs to know can be set as globals. It is good practice to use - as a rule of thumb - globals only for constants. You can manipulate such constants via the dot-execution method, but in most cases this is a bad idea because it indeed introduces a side effect of the execution of the function.
In general it is a good idea to pass all the information a function needs to know as a parameter to it. I do usually create a "function outline" before i even write it by simply asking what the function needs to know (the parameter set) and what it needs to give back. This way, when i start actually writing the function, i already have the "interface" of what it has to look like to the outside ready.
Here is an example: i once wrote a function for distributed execution of commands (via ssh-sessions). Now i thought: what do i need to know?
1) the host on which to execute
2) the command to execute
3) the use which to use for the connection
4) the user under which to execute
What do i need to give back to the calling routine?
1) The error level of the command executed
2) any output it might produce (stdout and stderr)
3) a return value of the function itself in case something goes wrong (host refused connection, etc.)
So i could write a function outline like that:
Things to do (obviously): catch the output, check if a connection is possible (system can be pinged, ssh-keys are exchanged, ...) and so on ... but this is implementation, not design.
For things a function has to manipulate (instead of just know) there are two ways: first, you can catch the <stdout> of a function:
OpenBSD complains when it sees this function in my program
/*This function takes the string "test\n" and returns the string "test\n\test\ntest\n"
ENTROPY = 1024
*/
void *build_string(int count, char **strarr)
{
int k;
char *new;;
size_t max;
if(count == 0) {
... (2 Replies)
hi
how can I return multiple values from a C function. I tried the following:
#include <stdio.h>
void foo(int id, char *first_name, char *last_name)
{
/*
this is just an example to illustrate my problem... real code makes
use of the "id" parameter.
*/
first_name = (char... (8 Replies)
Hi,
I have a script which does something like the below:
execute_some_script.sh $arg1 $arg2 `exec-some-cmd`
if then;
do something
else
do something else
fi
However, during some cases, there is an error saying:
line xxx: [: too many arguments
at the line number which has... (5 Replies)
Hi there, I have the following output,
# raidctl -l
RAID Volume RAID RAID Disk
Volume Type Status Disk Status
------------------------------------------------------
c0t1d0 IM OK c0t1d0 OK
... (4 Replies)
Hi,
I need to retrun multiple values
function errorFileCreation
{
echo "Before"
return -1 "Siva";
echo "Aftyer"
}
echo ${?} - This can be used to getting first value.
how can i get second one.
Advance Thanks...
Shiv (3 Replies)
I know multiple values can be returned from a function in C like this:
char **read_file ( char * , unsigned long int * );//this is the function prototypeunsigned long int number_of_words = 0;//variable defined in main() and initialized to 0words_from_dictionary = read_file ( "dictionary.dit" ,... (2 Replies)
Hello,
i'm trying to implement the times() function and i'm programming in C.
I'm using the "struct tms" structure which consists of the fields:
The tms_utime structure member is the CPU time charged for the execution of user instructions of the calling process.
The tms_stime structure... (1 Reply)
Hello!
I have one strange question - let's say I have a long, multiple-line string displayed on the terminal using echo, and I would like to make a carriage return to the beginning of this string, no to the beginning of the last line - is something like that possible? I would like to be able to... (1 Reply)
I am using a for loop to copy files from say DIR1 and DIR2 to DIR3.I have to check whether files are copied from DIR1 and DIR2 and print the respective message.
@path=("$DIR1","$DIR2");
foreach (@path) {
$rc=system("cp $_/*xml $DIR3");
if ($rc == 0)
{
print "Files were copied... (1 Reply)
Hi
I am pretty confused in returning and capturing multiple values
i have defined a function which should return values "total, difference"
i have used as
#!/usr/bin/ksh
calc()
{
total=$1+$2
echo "$total"
diff=$2-$1
echo "$diff"
}
I have invoked this function as
calc 5 8
Now i... (2 Replies)