ATAR Notes: Forum

VCE Stuff => VCE Technology => VCE Mathematics/Science/Technology => VCE Subjects + Help => VCE Computing: Software Development => Topic started by: Nephix on November 01, 2013, 08:31:40 pm

Title: Functions
Post by: Nephix on November 01, 2013, 08:31:40 pm
Hey guys how's it going?

I was wondering if any of you SD dudes could help me out with explaining what a function is?

I have looked on both VCE IT and in the Janson's texbook and I really don't follow (programming isn't the best)

All I have gathered is that a function returns a 'value' , and is much the same as a procedure except for this exception.

What does it mean by returning a value? I thought calling a subroutine sort of returned a value. But I'm pretty bad at programming.

Title: Re: Functions
Post by: Lasercookie on November 02, 2013, 08:49:31 pm
A subroutine is a block of code that doesn't have to return a value. A function is a subroutine (a block of code) that does return a value.

Say you have a program with some subroutine:
Code: [Select]
stuff;
call the subroutine;
more stuff;
So the program is going to do stuff, and then the subroutine is called. It's going to jump to the subroutine and run that block of code. Once it's done going through the subroutine, it'll jump back to the program and do more stuff.

Lets say that our subroutine is now a function. Returning a value means that means that once the program is done running the subroutine code, when it's jumping back to the program, it's also going to pass a value back to the program. The program will then be able to use this value.

Say we have some function temperature() defined, and it returns an integer value. If we were to define a variable as aVariable = temperature(parameter1, parameter2), it's value would be set to whatever integer that function returned. Or if you had something like 'print temperature(parameter1, parameter2)', then it would print the integer value that the function returned.

There's also the built-in functions of programming languages that you've probably been using too, which also return values. An example of a built-in function might be something that you pass an array to, and then it will count and return the number of elements in that array.

Study design wise, this comes under the dot point "processing features of programming languages, including instructions, procedures, methods, functions and control structures". Note that a procedure is a synonym for a subroutine.
Title: Re: Functions
Post by: Nephix on November 02, 2013, 10:44:36 pm
 Awesome, thanks dude ^^
Title: Re: Functions
Post by: MJRomeo81 on November 04, 2013, 10:31:02 pm
For those of you that want to see an example in real code (Java for example):

A procedure is a block of code (subroutine) that doesn't return a value.

public void doThis()

So this block of code will execute, do things, print stuff to the screen, etc. but doesn't pass a value to another subroutine.

However a function returns a value.

public double calculatePerimeter(double length, double width)
Title: Re: Functions
Post by: Ya Habibi on November 13, 2013, 05:47:36 pm
For those of you that want to see an example in real code (Java for example):

A procedure is a block of code (subroutine) that doesn't return a value.

public void doThis()

So this block of code will execute, do things, print stuff to the screen, etc. but doesn't pass a value to another subroutine.

However a function returns a value.

public double calculatePerimeter(double length, double width)

Not necessarily. Yes, a function will return a value. A procedure requires an input, but won't always give an output. This is from the answers to one of the questions on the VITTA exams.
Title: Re: Functions
Post by: MJRomeo81 on November 13, 2013, 10:26:52 pm
A procedure requires an input

A procedure doesn't necessarily require input. Consider the case where I have a procedure to print to the screen the times tables 1-12 using for loops.

i.e. the method (in Java) could be:

public void printTimesTables() // notice how it takes no parameters
Title: Re: Functions
Post by: Ya Habibi on November 13, 2013, 10:32:31 pm
But couldn't I rebut that by saying an input, such as a mouse click or form initialisation can be referred to as an input?
Title: Re: Functions
Post by: MJRomeo81 on November 13, 2013, 10:44:17 pm
You would certainly be justified in making that statement. However, VCAA tend not to ask such specific details regarding procedures/functions. (Technically my statement still stands. I could have a private procedure that isn't accessible by mouse click and it appears on the same form, and I simply call the procedure from another one).

A relevant question (and one I liked) popped up in the 2011 exam:

Question 9

In a program, a line of code that modifies a variable's content is best described as a

A.  function.
B.  procedure.
C.  instruction.
D.  control structure.
Title: Re: Functions
Post by: no steez on November 13, 2013, 11:42:41 pm
You would certainly be justified in making that statement. However, VCAA tend not to ask such specific details regarding procedures/functions. (Technically my statement still stands. I could have a private procedure that isn't accessible by mouse click and it appears on the same form, and I simply call the procedure from another one).

A relevant question (and one I liked) popped up in the 2011 exam:

Question 9

In a program, a line of code that modifies a variable's content is best described as a

A.  function.
B.  procedure.
C.  instruction.
D.  control structure.
is it C?
Title: Re: Functions
Post by: MJRomeo81 on November 14, 2013, 12:03:21 am
Correct, C is the answer :)
Title: Re: Functions
Post by: speedy on November 14, 2013, 04:33:53 pm
So is an instruction just a single line of code? Or does it have to modify a variable in some way?
Title: Re: Functions
Post by: darvell on November 14, 2013, 05:37:25 pm
So is an instruction just a single line of code? Or does it have to modify a variable in some way?

Just a single line of code, doesn't necessarily need to modify a variable.

Eg: System.out.println("Software Development");
Is an instruction - doesnt modify variables, simply prints what we've asked it to.
Title: Re: Functions
Post by: speedy on November 14, 2013, 05:51:51 pm
Just a single line of code, doesn't necessarily need to modify a variable.


Thanks, that's what I thought  :D