Functions
Assumed Knowledge
Learning Outcomes
- Know the syntax of functions
- Understand how functions relate to algorithms
- Be able to pass values into a function
- Be able to get values back from a function
- Be able to write your own functions
- Be able to reason about the flow of a program that uses functions
A function is a named piece of code that can be supplied with some inputs (known as parameters) and may return a value back to the caller.
Chapter 7 of Learning Processing by Danel Shiffman.
Details
Let’s be precise about functions.
For example, we may have a function that determines the higher of two integers. We can call it by passing it two integers. If we pass the values 2 and 5, it should return 5.
However, if we pass only one value, it cannot be executed, as it expects two integers.
Similarly, you cannot pass it more than 2 values.
Even if you pass two values, you must ensure they are of the right type. For example, we cannot pass a boolean
instead of an integer.
Defining a function
All functions have just one function definition - the place that the function itself is described. Syntax of a function definition is:
returnType function(<parameters>) {
<some code>
<if>
<return statement>
<endif>
<some code>
<return statement>
}
Once this exists, you can have many function calls - the place that the code asks the function to run. Syntax of a function call is:
functionName (<parameters>);
For example, the following function definition
int foo(int a, int b){
return a + b;
}
can be run by any of these function calls
int t = foo(1,3);
int y = foo(12, 13);
int z = foo(foo(1,3), 8)
Note the feature in the above example, anywhere an int
is expected, you can put a calls to foo
because it returns an int
.
Formal and Actual parameters
Since we have both function definitions and function calls, and parameters appear in each, it is useful to be able to distinguish the two uses.
- Parameters appearing in the top line (signature) of a function definition are called formal parameters and are defined in the same way as any other variable declaration
- Parameters appearing in a function call are called actual paramters and work like any other value in Processing.
When the function is executed, the actual paramters are copied into the formal paramter slots and the function is run.
Suppose we have a function that accepts a real number (double
) and returns its square.
Draw a block diagram for the interaction when a caller calls the function with the value 2.5. Assume the name of the formal parameter is val
, and the value returned by the function is copied into a variable sqr
.
Solution
Consider the following function definition,
int roundOff(float a) {
return (int)(a+0.5);
}
Write a statement that calls the function roundOff
with the parameter 6.8 and stores the value returned in a variable result
.
Solution
int result;
result = roundOff(6.8);
Which of the following are valid calls to function roundOff
?
int a = roundOff(4.5);
int b = roundOff(8);
roundOff(2.6);
float c = roundOff(-1.53);
int d = roundOff(3.2, 4.8);
int e = roundOff();
int e = roundOff(true);
Solution
- Yes
- Yes, the integer is treated like a float when it arrives in the formal parameter. It is identical to writing
float a = 8
- Yes, the return value is thrown away, but that is allowed
- Yes, the return value is treated like a float when it arrives in
c
- No, too many actual parameters
- No, not enough acutal parameters
- No, actual parameter type does not match formal parameter type.
Define a function that when passed two integers, returns their average. Remember that 15/2 is 7 while 15/2.0 is 7.5.
Solution
float average(int a, int b){
return (a + b)/2.0;
}
Statements or Expressions?
- function declarations are statements.
- function calls are statements if the function returns
void
. - function calls are statements and expressions if they return a value.
- formal parameters are statements.
- actual parameters are expressions.
The fact that a function call in processing can do something and also evaluate to a value makes them special. In fact, this double-life is a bit of a point of friction in language design. Some languages insist that functions must be statements and others insist they must be expressions, but those are rare. The situation we have here, while strange, is quite common.
Furthering your Understanding
Define a function that when passed an integer, returns the number of digits in the integer.
Solution
int digits(int input){
int totalSoFar = 0;
while (input > 0) {
totalSoFar++;
intput = input / 10;
}
return totalSoFar;
}
Given two integers (store in formal parameters a, b
), define a function that determines if either of them is divisible by the other. Some input-output mappings are:
a = 14, b = 6
–> returnfalse
a = 14, b = 7
–> returntrue
a = 9, b = 30
–> returnfalse
a = 9, b = 36
–> returntrue
a = 12, b = 0
–> returntrue
(0 is divisible by 12)
Solution
boolean isDivisible(int a, int b){
return (a % b) == 0;
}
An year is leap if it satisfies one of the two conditions,
- it’s divisible by 400, or,
- it’s divisible by 4 but NOT by 100.
Define a function that determines if an year passed (store in formal parameter year
) is leap
. Return true
if it’s a leap year, and false
otherwise. Some input-output mappings are:
year = 2016
–> returntrue
year = 1800
–> returnfalse
year = 2018
–> returnfalse
year = 1600
–> returntrue
Solution
boolean isLeapYear(int year){
return (isDivisible(year, 400) || (isDivisible(year, 4) && !isDivisible(year 100)));
}
I would like to count the number of non-trivial (apart from 1 and itself) divisors of a given integer. Some input-output mappings are:
n = 18
–> return4
(as there are 4 non-trivial divisors: 2, 3, 6, 9)n = 31
–> return0
(as there is no non-trivial divisor of 31)n = 77
–> return2
(as there are 2 non-trivial divisors: 7, 11)
Solution
int divisors(int input){
found = 0;
for(int i = 2; i < input; i++){
if (isDivisible(input, i){
found++;
}
}
return found;
}