Functions in JavaScript - Example error

Hi,

am getting an error undefined when tried the lessons sample code, and I could’t find any mistakes, please help.

<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<title></title>
	<script type="text/javascript">
		
		function pi(){
			return 3.14;
		}
		function area_of_circle(radius){
			area= pi() * radius * radius;
		}

		alert(area_of_circle(5));
	</script>
</head>
<body>

</body>
</html>

You have not returned the area, kindly have a look at the code below.

function pi(){
			return 3.14;
		}
function area_of_circle(radius){
	area= pi() * radius * radius;
    return area;//return the area
}

alert(area_of_circle(5));
1 Like

Thanks you very much.

1 Like