Functions
Note: This lesson covers defining functions in JavaScript and Python.
Definition
A function
is a block of reusable code designed to perform a particular task. The function is executed when it is "called".
Syntax
functions
function function_name(parameter1, parameter2, ... parameterN) {
block;
}
- A JavaScript function is defined using the
function
keyword, followed by the function name, followed by parentheses (). The parentheses () may contain a list of parameters. - The code to be executed by the function is placed inside curly brackets.
def function_name(parameter1, parameter2, ... parameterN):
block
- A Python function is defined using the
def
keyword, followed by the function name, followed by parentheses () ending with a colon. The parentheses () may contain a list of parameters. - The code to be executed by the function is indented.
Examples
Example 1: Without Parameters
A function message()
is defined. The parentheses () are empty as this function does
not use parameters. This means the function is not passed data to operate on.
When the function is called, i.e., message()
, it displays a simple message 'Hello'.
This is a simple example. When you create functions you can optionally define:
- if the function should be passed data to operate on (Example 2: With Parameters);
- if the function should return a value (Example 3: Returning Results).
- Defining a function
function message() { alert("Hello"); }
- Calling a function
message(); // Hello
function
keyword, followed by the function
name message
, followed by parentheses (). The function code block is placed inside curly brackets.
This function uses an alert to display a simple message.
- Defining a function
def message(): print("Hello")
- Calling a function
message() # Hello
def
keyword,
followed by the function name message
, followed by parentheses () ending with a colon.
The function code block is indented.
Example 2: With Parameters
Here the function will be passed data to operate on. We define a function greet
with a parameter named message
.
When the function is called, the function assigns the data
passed to it, i.e., morning
, afternoon
or evening
to the parameter message
.
Note the difference between an argument and parameter.
- Argument: A value passed to a function when the function is called. E.g.,
greet("morning");
. - Parameter: the variable listed inside the parentheses in the function definition -
used to refer to the value passed to it. E.g.,
message
.
Functions can be defined with a list of parameters by separating each parameter name with a comma.
- Defining a function
function greet(message){ alert("Good " + message); }
- Calling a function
greet("morning"); // Good morning greet("afternoon"); // Good afternoon greet("evening"); // Good evening
- Defining a function
def greet(message): print("Good " + message)
- Calling a function
greet("morning") # Good morning greet("afternoon") # Good afternoon greet("evening") # Good evening
Example 3: Returning Results
- To allow a function to return a value, use the
return
statement. - The
return
statement immediately terminates a function execution and sends the return value back to the code that called the function. Therefore, line 3 will not run. - We save the returned value in a variable
result
. Then we simply display the value returned and saved toresult
.
function square(x){
return x * x;
alert("this line will never be printed");
}
var result = square(10);
alert(result);
def square(x):
return x * x
print("this line will never be printed")
result = square(10)
print(result)
Example 4: Returning Ticket Price
- Here is another example that returns results. We have taken the code used in
the
if else
lesson (Example 1: Ticket Price) and rewritten it to use a function. - The function accepts a value for
age
and calculates and returns the appropriateentry
message. - The returned value is displayed.
- This code could be modified to accept the age value from keyboard input.
function ticket(age){
var entry = "";
if ( age <= 12 ) {
entry = "Free";
} else if ( age <= 16 ) {
entry = "£10";
} else {
entry = "£20";
}
return entry;
}
alert(ticket(10)); // Free
alert(ticket(14)); // £10
alert(ticket(21)); // £20
def ticket(age):
entry = ""
if age <= 12:
entry = "Free"
elif age <= 16:
entry = "£10"
else:
entry = "£20"
return entry
print(ticket(10)) # Free
print(ticket(14)) # £10
print(ticket(21)) # £20