Reword this essay (Java) The following are Procedures: public static void setMyField(MyRecord r, int x) { /*...*/ } public static void printMyRecord(MyRecord r) { /*...*/ } public static void initMyRecordArray(MyRecord[] rs) { /*...*/ } Justification: the above code blocks identified by names setMyField , printMyRecord and initMyRecordArray both perform some processing(assumed from the body block) and have a return type of void implying that each of them does not return anything to its caller. Thus, they are all procedures since procedures do not return a value to their caller, can take parameters and perform some task. The following are functions: public static int getMyField(MyRecord r) { /*...*/ } public static String toString(MyRecord r) { /*...*/ } Justification: getMyField returns an integer to its caller and toString returns a string to its caller, take parameters and perform some
Identify which of the below are functions and which are procedures as defined in this
module, and justify your answers.
Explain in your own words the purpose of procedures in programs in general.
Illustrate your answer using the code below.
Assume an appropriate type MyRecord has been defined (you do not need to give a
definition). You may also interpret the /*...*/ snippets as standing for any code
you consider appropriate for the situation.
Reword this essay (Java)
The following are Procedures:
public static void setMyField(MyRecord r, int x) { /*...*/ }
public static void printMyRecord(MyRecord r) { /*...*/ }
public static void initMyRecordArray(MyRecord[] rs) { /*...*/ }
Justification: the above code blocks identified by names setMyField , printMyRecord and initMyRecordArray both perform some processing(assumed from the body block) and have a return type of void implying that each of them does not return anything to its caller. Thus, they are all procedures since procedures do not return a value to their caller, can take parameters and perform some task.
The following are functions:
public static int getMyField(MyRecord r) { /*...*/ }
public static String toString(MyRecord r) { /*...*/ }
Justification: getMyField returns an integer to its caller and toString returns a string to its caller, take parameters and perform some task from the implied body block hence they are both functions.
The return type of int for getMyField function implies that given an argument of type MyRecord, the function will perform some processing and upon exit return an integer value to its caller such as the main function. Hence, getMyField is a function because it return a value after execution.
The return type of String from the function toString implies that given an argument r of type MyRecord. The function will return a value of data type String to its caller. Hence toString is a function since a function is expected to return value upon exit.
Purpose of procedures in programs in general:
Procedures help support code reuse and decomposition of the program structure by breaking of the structure into related tasks. Can also can reduce the amount of program code written, speed up
Illustration from supplied code:
setMyField procedure for instance can be concerned with setting an integer x as some value in the variable r of type MyRecord.
printMyRecord procedure can be concerned with displaying the data inside the variable r of type MyRecord.
initMyRecordArray procedure can be concerned with initializing an array rs of type MyRecord supplied as an argument.
All the three procedures perform separate tasks and can be easily reused for instance in a loop. Thus this reduces the amount of code to be written reducing program size and development time. Errors durting initialization can be attributed to the initialization procedure thus improving debugging. Different programmers can also be assigned different procedures/ tasks thus supporting collaboration among other purposes during program development.
Step by step
Solved in 2 steps