In many languages as in Java you can choose whether a procedure called a method

In many languages as in java you can choose whether a

This preview shows page 36 - 46 out of 77 pages.

36 §In many languages (as in Java) you can choose whether a procedure (called a methodin O-O languages) has a return value or not §If it does have a return value, then you will often see calls to it like chr = firstCharOf( str); so the return value of the procedurefirstCharOf() is assigned to the variable chr Or, calls like print( firstCharOf( str )); Return Types
Image of page 36
37 § So the return value of firstCharOf() is given to the procedure print() § In typed languages like Java (which require you to declare the type of variables) you will need to declare the return type of a procedure in its definition and make sure it makes sense where it is called § In some languages procedures with return values are called functions Return Types
Image of page 37
38 §In the definition of a procedure with a return value you will need to indicate exactly which value to return to the main program or another procedure. For example, in Java you write: return 2; to immediately send the value 2 back from the procedure §Use "return value" also in pseudo-code. Make sure that the type of the value matches the declared return type of the procedure Return Types
Image of page 38
39 § Here is the possible pseudo-code for the whole of the procedure lastCharOf() Procedure char lastCharOf( String str ) n = lengthOf(str) lco = CharAt( str, n-1 ) return lco § Again this is high-level § However, I happen to know that it uses procedures which are (practically) built-in to Java § They find the length of a String and find the character at a given index location in a String Example
Image of page 39
40 § As in many languages, Java indexes String (and Array) locations starting from 0 § So the first character is at index 0, etc. § If you want to know how these procedures themselves are defined in terms of even more basic steps then you need to start thinking about the way Strings are implemented § But that is Data Abstraction and it is looking below Java anyway, so we won’t Example
Image of page 40
41 § Note there is no official syntax (way of writing) procedures in pseudocode, but do try to be clear about names, types, etc. Example
Image of page 41
42 § If the procedure has no return value then it can only be called via a whole statement like: print(‘x’); or print( firstCharOf( str ) ); i.e. it does not make sense to write a = print(‘x’); § In Java these types of procedures are treated in a similar way to procedures with a return value. We just declare the return type to be void if we have no return value Procedures With No Return Types
Image of page 42
43 § You can put a return statement in the definition of the procedure to indicate where control passes back to the main program § Remember that code might (or might not) be useless after a return statement: void proCC( int x) { if (x != 3) { x = 4; } else return; Procedures With No Return Types
Image of page 43
44 printOut(x); return; printOut(x-1); } //end of proCC § Sometimes, you do not have to put a return § The procedure also returns when its end is reached Procedures With No Return Types
Image of page 44
45 § Information is also passed in and out of a procedure via its arguments § You will need to specify the number of, types of and exact ordering of arguments § You do this in the procedure definition by using some formal parameters , i.e. variables standing for the arguments (actual parameters) which you can then use in the body of the procedure Formal Parameters and Arguments
Image of page 45
Image of page 46

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture