# Defining Simple Functions

Practice Labs Module
Time
18 minutes
Difficulty
Intermediate

Welcome to the Defining Functions Practice Lab. In this module, you will be provided with the instructions and devices needed to develop your hands-on skills.

Join over 3 million cybersecurity professionals advancing their career
or

Overview

### Introduction

Welcome to the Defining Functions Practice Lab. In this module, you will be provided with the instructions and devices needed to develop your hands-on skills.

### Learning Outcomes

In this module, you will complete the following exercise:

• Exercise 1 - Implementing a Calculator Using Functions

After completing this lab, you will be able to:

• Structure Python programs by defining functions

### Exam Objectives

The following exam objectives are covered in this lab:

• 4.1 Construct and analyze code segments that include function definitions

### Lab Duration

It will take approximately 15 minutes to complete this lab.

### Exercise 1 - Implementing a Calculator Using Functions

A function is a block of program statements, which can be defined once and called repetitively in a program. You have been using built-in functions like print() all this while. This function helps you print any output in no time. However, not all your needs may be met through built-in functions. In these cases, Python allows you to define your own functions.

In Python, a user-defined function is declared with the keyword def and followed by the function name. For example:

def myfunc():

A colon follows the closing parenthesis to suggest that the following block of code will contain the function definition.

Any arguments are specified within the opening and closing parentheses just after the function name, as shown here:

def myfunc(arg1, arg2):

After defining the function name and arguments(s), a block of program statement(s) start at the next line and these statement(s) must be indented. For example:

def myfunc(arg1, arg2):

print (arg1)

print (arg2)

The above function named myfunc() takes two arguments and prints their values.

Once defined, you can call this function from within the Python program using the function name, parenthesis (opening and closing) and parameter(s). For example, to call the myfunc() function, you may use the following statement:

myfunc(3, 4)

The above statement will call the myfunc() function with 3 and 4 as arguments and the function will print 3 and 4 as the output.

You can call the same function to print the string “hello world” too, as follows:

myfunc(“hello”, “world!”)

A user-defined function can also return a value instead of directly printing the result. In Python, the return statement is used to return a value from a function. The return statement without an expression argument returns none.

If you modify the myfunc() function to return the parameters after adding number 1 to each parameter, the function will look as follows:

def myfunc(arg1, arg2):

newarg1 = arg1 + 1

newarg2 = arg2 + 1

return(newarg1, newarg2)

Now in this case, the myfunc() function will not print the result on its own. It will just return two values newarg1 and newarg2. The above function can be called and results can be printed using the following combined statement:

print(myfunc(3, 4))

The above statement will call the myfunc() function with 3 and 4 as arguments. This print function will print the values returned by the myfunc() function.

A user-defined function can be called multiple times. Your own user-defined functions can also be a third-party library/module for other users. In addition to fostering reusability and saving development time, user-defined functions help make the code well organized, easy to maintain, and developer-friendly.

In this exercise, you will perform three tasks. In the first task, you will download the Python program file from the Intranet and open it in the IDLE environment. In the second task, you will write specific functions to perform different mathematical operations on the numbers accepted at runtime and print the results. In the third task, you will modify the program to have the user-defined functions return values instead of directly printing them.

### Learning Outcomes

After completing this exercise, you will be able to:

• Structure Python programs by defining functions
Learning Partner
Comprehensive Learning

See the full benefits of our immersive learning experience with interactive courses and guided career paths.