In this lesson, we will learn about functions in a program, with a focus on the following key points,
- What are functions, and why do we use them?
- Working with function parameters, return, and call statements.
For the Javascript version of this lesson, please click here.
In this lesson, we will learn about a fundamental concept in programming called functions
. Functions provide a modular structure to the code. They perform a single action and provide a method to reuse a code block in different parts of the program. Functions are known by various terminologies in computer science. Methods
, procedures
, and subroutines
are some of the common ones.
Functions
A function
is a block of code in a programming language that encloses a set of statements. Once implemented, this block of code is reusable throughout the program. Functions make it easier to organize and reuse code.
A function generally follows the following structure in a program.

Let's discuss each of these components of functions in the next sections.
Types of Functions
In programming languages, there are usually two types of functions; built-in functions
and user-defined functions
. Built-in functions are pre-defined by the programming language itself. We've already seen built-in functions in previous lessons such as print()
. User-defined functions are functions that are defined by the user to dedicate a block of code to perform a specific task.
In this lesson, we will discuss how you can create and use your functions within your program.
Creating Functions
Function declaration or creating functions is different in different programming languages. Usually, a function keyword
is specified, followed by the function name
, parenthesis
(with function parameters
, if any), and then comes the body of the function
.
Now let's see how we can create functions in Python.
In Python, the def
keyword is used as the function keyword. This is followed by the function name, parenthesis, and a colon. The body of the function is denoted by an indentation.
xxxxxxxxxx
function javascriptFunc() {
console.log("Hello World!");
}
javascriptFunc();
Calling Functions
In our last code example we created a function, but as you may have checked, it did not produce any output. Why? Because functions need to be invoked by a call to use them in a program. Calling a function means to specify the function name of the function that we want to use in the program. This executes all the statements present inside the function.
Consider the same example as before. Now we specify a statement which denotes the function name along with the parenthesis, after the function implementation. This statement invokes the call to the function. The code block below demonstrates this behavior.
xxxxxxxxxx
function javascriptFunc() {
console.log("Hello World!");
}
javascriptFunc();
Are you sure you're getting this? Is this statement true or false?
We can call a function before implementing it.
Press true if you believe the statement is correct, or false otherwise.
Function Parameters
Functions can take in external information in the form of variables or directly through function parameters
. They are specified inside the parenthesis after the function name. Multiple parameters can be separated by commas.
Function parameters are given a variable name inside the parenthesis during function declaration. The desired values are passed when the function call is invoked. Below code block demonstrate some examples of specifying function parameters and calling functions with parameters.
1function func1(name) {
2 console.log("Hello I am " + name);
3}
4
5function add(a, b, c) {
6 let d = a + b + c;
7 console.log("Sum of a, b, and c is: " + d);
8}
9
10func1("Beth");
11add(2, 3, 4);
Note: Here the variable
d
is of typeint. We use
str(d)to convert the type of variable
dfrom
intto
string, as string concatenation only works with two variables of type
string. Concatenation of
stringand
int` would generate an error.
Are you sure you're getting this? Is this statement true or false?
There is no limit to the number of parameters that we can specify for a function.
Press true if you believe the statement is correct, or false otherwise.
Returning Functions
Functions stop executing at two points; when it finishes executing all statements in the function body or when it encounters a return
statement. This return
statement mostly also has a return value
associated with it, which is returned to the caller.
The function add(a, b, c)
in the following code block implements the sum of three numbers and returns the sum. This sum is then returned to the variable that calls the function, which is named as result
here. Printing the value of result
tells us the value of the sum computed by the function.
xxxxxxxxxx
function add(a, b, c) {
var d = a + b + c;
return d;
}
var result = add(33, 12, 47);
console.log(result); // prints 92
Are you sure you're getting this? Fill in the missing part by typing it in.
What will be printed by the variable result
, in the following code block in Python?
1function div(a, b) {
2 let c = b / a;
3 return c;
4}
5
6let result = div(15, 5);
7console.log(result);
Write the missing line below.
Let's test your knowledge. Click the correct answer from the options.
What is the output for the following code?
1function compareNum(a, b) {
2 if (a > b) {
3 console.log("a is greater than b");
4 return a;
5 } else if (a === b) {
6 console.log("a is equal to b");
7 return a;
8 } else {
9 console.log("a is less than b");
10 return b;
11 }
12}
13let result = compareNum(4, 2);
14console.log(result);
Click the option that best answers the question.
- 'a is greater than b', 2
- 'a is less than b', 2
- 'a is greater than b', 4
- 'a is equal to b', 4
Summary
Functions are an extremely helpful technique to reuse and organize code. With specifying parameters and return statements, we can do more with functions that are just combining a set of statements, and even specify different values from the entirety of the program that we want to work upon. A common beginner's mistake is to forget to call the implemented functions when using them in code. Don't fall for it!
One Pager Cheat Sheet
- We will learn about functions, their
parameters
and working statements to create modular and reusable code blocks. - Functions
enclose a set of statements
tomake it easier to organize and reuse code
, following a specific structure. - We will learn to create and use our own user-defined functions in our program, as well as review existing built-in functions.
- Creating functions in Python requires a
def
keyword followed by the function name,parenthesis
(withfunction parameters
, if any), a colon, and an indentedbody
. - We can make a function work in our program by
calling
it with itsname
, followed by parentheses( )
. - It is not possible to call a function before it has been defined and implemented with all its
statements
andparameters
established. - Functions can take in external information through function parameters specified inside the parenthesis after the function name, which is then passed to the function call when invoked.
- There is no upper limit to the number of
function parameters
that can be specified, provided they are separated correctly and referenced correctly within the function. - A
return
statement in a function can allow the function to pass back areturn value
to the variable that called it. - The variable
result
is set toNone
implicitly returned by thediv()
function, resulting inNone
being printed when printing the value ofresult
. - The variable
result
contains4
, the larger value returned by the functioncompare_num()
, which comparesa
(4) andb
(2) and prints out "a is greater than b" whena
is the greater value. - With
functions
, we canreuse and organize
code more efficiently, avoiding the mistake of forgetting to call them.