Login

Welcome, Guest. Please login or register.

September 09, 2025, 05:29:15 am

Author Topic: Functions  (Read 3819 times)  Share 

0 Members and 1 Guest are viewing this topic.

Nephix

  • Victorian
  • Adventurer
  • *
  • Posts: 19
  • Respect: 0
Functions
« on: November 01, 2013, 08:31:40 pm »
0
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.


Lasercookie

  • Honorary Moderator
  • ATAR Notes Legend
  • *******
  • Posts: 3167
  • Respect: +326
Re: Functions
« Reply #1 on: November 02, 2013, 08:49:31 pm »
+3
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.

Nephix

  • Victorian
  • Adventurer
  • *
  • Posts: 19
  • Respect: 0
Re: Functions
« Reply #2 on: November 02, 2013, 10:44:36 pm »
0
 Awesome, thanks dude ^^

MJRomeo81

  • Part of the furniture
  • *****
  • Posts: 1231
  • Princeps
  • Respect: +167
Re: Functions
« Reply #3 on: November 04, 2013, 10:31:02 pm »
+1
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)
Currently working in the IT Industry as an Oracle DBA (State Government)

Murphy was an optimist

Bachelor of Information Technology @ La Trobe (Melbourne) - Completed 2014
WAM: 91.96
The key, the whole key, and nothing but the key, so help me Codd.

Subjects I tutored during my time at LTU:
CSE2DBF (Database Fundamentals)
CSE1IS (Information Systems)
CSE2DES (System Design Engineering)

Quote
“If I had an hour to solve a problem I'd spend 55 minutes defining the problem and 5 minutes thinking about solutions.”
― Albert Einstein

Ya Habibi

  • Victorian
  • Forum Regular
  • **
  • Posts: 53
  • Respect: 0
  • School Grad Year: 2014
Re: Functions
« Reply #4 on: November 13, 2013, 05:47:36 pm »
0
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.
« Last Edit: November 13, 2013, 06:17:31 pm by Ya Habibi »
2013 Subjects: IT: Software Development (38)

2014 Subjects: Math Methods (36), English (44), French (35), Physics (36), Chemistry (38)

ATAR: 96.85

MJRomeo81

  • Part of the furniture
  • *****
  • Posts: 1231
  • Princeps
  • Respect: +167
Re: Functions
« Reply #5 on: November 13, 2013, 10:26:52 pm »
0
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
Currently working in the IT Industry as an Oracle DBA (State Government)

Murphy was an optimist

Bachelor of Information Technology @ La Trobe (Melbourne) - Completed 2014
WAM: 91.96
The key, the whole key, and nothing but the key, so help me Codd.

Subjects I tutored during my time at LTU:
CSE2DBF (Database Fundamentals)
CSE1IS (Information Systems)
CSE2DES (System Design Engineering)

Quote
“If I had an hour to solve a problem I'd spend 55 minutes defining the problem and 5 minutes thinking about solutions.”
― Albert Einstein

Ya Habibi

  • Victorian
  • Forum Regular
  • **
  • Posts: 53
  • Respect: 0
  • School Grad Year: 2014
Re: Functions
« Reply #6 on: November 13, 2013, 10:32:31 pm »
0
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?
2013 Subjects: IT: Software Development (38)

2014 Subjects: Math Methods (36), English (44), French (35), Physics (36), Chemistry (38)

ATAR: 96.85

MJRomeo81

  • Part of the furniture
  • *****
  • Posts: 1231
  • Princeps
  • Respect: +167
Re: Functions
« Reply #7 on: November 13, 2013, 10:44:17 pm »
0
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.
« Last Edit: November 13, 2013, 11:00:54 pm by MJRomeo81 »
Currently working in the IT Industry as an Oracle DBA (State Government)

Murphy was an optimist

Bachelor of Information Technology @ La Trobe (Melbourne) - Completed 2014
WAM: 91.96
The key, the whole key, and nothing but the key, so help me Codd.

Subjects I tutored during my time at LTU:
CSE2DBF (Database Fundamentals)
CSE1IS (Information Systems)
CSE2DES (System Design Engineering)

Quote
“If I had an hour to solve a problem I'd spend 55 minutes defining the problem and 5 minutes thinking about solutions.”
― Albert Einstein

no steez

  • Victorian
  • Forum Obsessive
  • ***
  • Posts: 257
  • p g
  • Respect: -2
  • School: Frankston Tafe
  • School Grad Year: 2013
Re: Functions
« Reply #8 on: November 13, 2013, 11:42:41 pm »
+1
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?
2013:

MJRomeo81

  • Part of the furniture
  • *****
  • Posts: 1231
  • Princeps
  • Respect: +167
Re: Functions
« Reply #9 on: November 14, 2013, 12:03:21 am »
0
Correct, C is the answer :)
Currently working in the IT Industry as an Oracle DBA (State Government)

Murphy was an optimist

Bachelor of Information Technology @ La Trobe (Melbourne) - Completed 2014
WAM: 91.96
The key, the whole key, and nothing but the key, so help me Codd.

Subjects I tutored during my time at LTU:
CSE2DBF (Database Fundamentals)
CSE1IS (Information Systems)
CSE2DES (System Design Engineering)

Quote
“If I had an hour to solve a problem I'd spend 55 minutes defining the problem and 5 minutes thinking about solutions.”
― Albert Einstein

speedy

  • Victorian
  • Forum Obsessive
  • ***
  • Posts: 336
  • Respect: 0
  • School Grad Year: 2014
Re: Functions
« Reply #10 on: November 14, 2013, 04:33:53 pm »
0
So is an instruction just a single line of code? Or does it have to modify a variable in some way?
Physics [50] | Chemistry [45] | English [42] | IT:SD [44]
ATAR: 98.95

darvell

  • Victorian
  • Forum Regular
  • **
  • Posts: 57
  • Respect: +25
  • School Grad Year: 2013
Re: Functions
« Reply #11 on: November 14, 2013, 05:37:25 pm »
+1
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.
Psych // English // Further Math // I.T Apps // I.T SoftDev

speedy

  • Victorian
  • Forum Obsessive
  • ***
  • Posts: 336
  • Respect: 0
  • School Grad Year: 2014
Re: Functions
« Reply #12 on: November 14, 2013, 05:51:51 pm »
0
Just a single line of code, doesn't necessarily need to modify a variable.


Thanks, that's what I thought  :D
Physics [50] | Chemistry [45] | English [42] | IT:SD [44]
ATAR: 98.95