RU beehive logo ITEC dept promo banner
ITEC 120
2008spring
ibarland,
jdymacek

homeinfoexamslectureslabshws
RecipeLawsliessyntaxjava.lang docsjava.util docs

lect01c
a second program
...and function syntax

Syntax for declaring functions

Recall the two functions we've seen so far: ufoHeightAt, and pizzaArea. From these two examples, can we figure out the general syntax needed whenever we write a Java function?

Note that when defining functions on your homework, you might prefer looking at the examples of other functions, or you might prefer the general-case syntax rules. It's a learning-style thing.

While we're talking about syntax, how about the syntax for calling a function? Quickly recalling the sorts of the expressions we typed in lab yesterday:

raki.ufoHeightAt(0)
jo.pizzaArea(10) + jo.pizzaArea(2*6)
jo.pizzaArea( Math.sqrt(64) )
We can give the general syntax as:

The above is official syntax; informally I find it's more helpful to memorize a couple of mantras:

Practice

Practice: Now it's your turn to write some functions.

Some self-study exercises

For the following problems, it's important to write test cases first, to really understand what is being asked. (Also, read the offer closely, and follow it exactly!)

Why re-use code?

The key feature to these programs is when we want to compute the area of (say) a 12″ pizza, we don't need to write 3.14*(12/2)*(12/2); we can write pizzaArea(12). What are the advantages of this approach?

This brings us to
The First Law of Programming: Do not repeat code, when you can re-use it.


1 For any numeric function, 0 and 1 are obvious inputs. consider:

There once was a girl from Purdue Whose limerick stopped at line two.
Or the more interesting test,
There once was a boy from Verdun
All good computer scientists also know there is a similar limerick about Nero, it goes without saying.      

2Can you spot the Pythagorean theorem at work, in jo.pizzaArea(12) + jo.pizzaArea(16) = jo.pizzaArea(20)?      

homeinfoexamslectureslabshws
RecipeLawsliessyntaxjava.lang docsjava.util docs


©2008, Ian Barland, Radford University
Last modified 2008.Jan.24 (Thu)
Please mail any suggestions
(incl. typos, broken links)
to iba�rlandrad�ford.edu
Powered by PLT Scheme