Who cares about math!!!

"Ricordatevi: l'algebra è come il porco, non si butta mai niente!"
(Maria Rosaria Celentani, Scienze MM.FF.NN)

LinuxBanner

Functions

Information can be passed to functions through arguments. An argument is just like a variable. Arguments are specified after the function name.

A function is a block of code designed to perform a particular task. A function is executed when "something" invokes it (calls it). Function parameters are the names listed in the function definition, usually. Bash use as parameters $n. Function arguments are the real values received by the function when it is invoked. Inside the function, the arguments behave as local variables.
fx for fx
for each value of "x" the function will return a different value.


#!/bin/bash function printTwoStrings { echo $1 $2 #parameters aren't listed #you can call them with $1 $2 #You must know, before to call the function, how it works. } printTwoStrings Hello Obistudends!

We had just replaced the "echo" function, with "printTwoStrings"
Apparently really useless! But...


#!/bin/bash function sum{ sum=$(( $1 + $2 )) echo $sum } sum 3 4


#!/bin/bash function sum { sum=$(( $1 + $2 )) echo $sum #is used to return value in standard output or to the main program } sum 3 4 sum 4 5 sum 7 9 # or better read -p "write the first number " a read -p "write the second number " b sum $a $b # or better for i in {0..9} do sum $i 1 done

So, how we can write the function, called pow for fx for fx


#!/bin/bash function pow { res=$(( $1*$1)) echo $res } read -p "insert a number : " a pow a


Best way to obtain the function "pow" is: fx for fx


#!/bin/bash function pow { res=1 for (( i=0; i<$2; i++ )) do res=$(( $res * $1 )) # echo $res done echo $res } read -p "insert a number : " base read -p "insert exp : " exp pow $base $exp


Functions must be as atomic as possible and as reusable as possible. Our goal is to write functions that we will be able to use at work, daily. So it is useless to write a function to obtain the sum of 2+3, or the pow of 2, but the sum of variables.
Variables, as we saw, can be integer or strings, so I'm pretty confident that you are already thinking about some useful idea...

Arguments or standard input?

We can send information to a script via standard input (using the read function), or passing it as argument once you are running it over the shell.



./runScript Argument1 Argument2


As for the functions, we can re-call arguments, into the script, with $n


./runScript Ciao Obilab #!/bin/bash echo $1 $2 #this will print the 2 arguments we've sent


Let's do some tests: array iteration with function


#!/bin/bash function printArray() { for (( i=0; i<$1; i++ )) do echo ${vet[$i]} done } vet=("17" "9" "14") # let's declare a simple array of 3 elements, INT n=3; # Number of elements of Array, as noobs does! tot=${#vet[@]} #Number of elements of Array, as shady does! #Print the Array # for (( i=0; i<tot; i++ )) #we can use "n" or "tot" # do # echo ${vet[$i]} # done clear echo now we will print the array printArray ${#vet[@]} ${vet[@]} echo "that's all!!"


So... are you ready for the final test? Let's figure it out!

Two other tips: I do love math, but... do not write codes as a math book. Variable, functions, must be readable, reusable with mnemonic names.



function x{ x=y/z*k-r echo x } x a b c d ## or worse than worse function x{x=y/z*k-r; echo x;}


It is the WORST method to implement a function, especially if you are working with other people, and if you will have to read your codes of 10.000 lines, after... 2 years!!!