954,546 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

problem with assigning variables inside a function

hey there...could I please ask for some help for my programming assignment. I having a problem in assigning values to a variables inside a function. I don't know what im doing wrong...any suggestions could be a great help..here is my code

a = 0

myfunc(){

echo $1 "Computation"
a = `expr 3.14\* $2\*$2|bc`

echo a


}

myfunc $1 $2

What I would like to do is to enter a command line argument. the first is a string and the second is a integer. the function should compute for the are of a circle but whenever I echo the value of a a error occurs..could I please ask for some clarifications on what Im doing wrong...thanks

If my arguments are Circle 2
The proper output of the program should be:


Circle Computation
12.56

herms14
Light Poster
47 posts since Mar 2008
Reputation Points: 10
Solved Threads: 0
 

expr doesn't handle decimals/floating numbers and your assignment syntax is wrong. Use var=value without the spaces. This fixes everything but expr . I don't know your project requirements but you will need to find another way to handle the math. Bash can handle math on its own by the way:

#!/bin/bash
a=0

myfunc(){

echo "Computation"
#a=`expr 3.14\* ${2}\*${2}|bc`
a=`echo 5`
echo ${a}
}

myfunc ${1} ${2}
sknake
Industrious Poster
4,954 posts since Feb 2009
Reputation Points: 1,764
Solved Threads: 735
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You