This document provides an introduction to functions in Python programming. It defines what a function is, how to define and call functions that do and do not return values, and how to work with local and global variables within functions. It also discusses passing arguments to functions and functions that return values. Examples are provided throughout to illustrate key concepts.
2. Abstract
▪ Introduction to functions
▪ Defining and calling functions (non-
returning and returning values)
▪ Local and global variables/constants
▪ Forwarding function arguments
3. Introduction to functions
▪ A function is a set of
commands that is used to
perform a certain task in a
program.
▪ The rationale for using the
function is the divide-and-
conquer rule (dividing the
program into smaller tasks)
▪ A distinction is made
between functions that
return a value and those
that do not return a value
Example:
command
command
command
command
command
command
command
command
command
Example:
def function1():
command
command
command
def function1():
command
command
command
def function1():
command
command
command
4. Defining and calling a function
that does not return a value
▪ Program code placed in a function
is called a function definition. To
execute a function, use the
command that calls it.
▪ Naming rules in Python: avoid
keywords, no spaces, python is
case sensitive, names refer to the
task
▪ In the first line is placed the
header of the function
▪ The set of commands below the
header is called a block
▪ block must be indented (each
line starts with the same
number of spaces)
Example:
def main():
print(’ I would like to introduce myself.’)
introduce_yourself()
def introduce_yourself():
print(’My name is Artur’)
main()
5. Local variables
▪ A local variable is declared
inside a function and cannot
be referenced outside of that
function (concept of function
scope).
▪ The coincidence of names
between local variables in
different functions does not
matter.
▪ Inside the function, the
place of the variable is also
important (you can refer to a
variable declared
earlier/higher in the code).
Example:
def main():
texas()
california()
def texas():
birds = 5000
print(’Texas has’, birds, ’bird species.’)
def california():
birds = 8000
print(’California has’, birds, ’bird species.’)
main()
6. Global variables and constants
▪ Global variables can be
used in all functions in a
program file. This is
possible when the variable
is created outside all
functions (see example 1).
▪ If you want to assign a
value to a global variable,
you must additionally use
the global command (see
example 2).
Example 1:
my_value = 10
def show_value():
print(my_value)
show_value()
Example 2:
Number = 0
def main():
global number
number = int(input(’Enter the number: ’))
show_number()
def show_number():
print(’The figure given is’, number)
main()
7. Forwarding function arguments
▪ An argument is the data
that is passed to a
function when it is called.
▪ A parameter is a variable
in which the argument
passed to the function is
stored.
▪ The scope of a parameter
usually includes the entire
function in which it is
defined (commands
outside the function
cannot access it).
▪ Passing several
arguments
▪ Arguments in the form of
keywords (see example
2)
Example 1:
def main():
print(’ 'The sum of the numbers is’)
show_sum(12, 45)
def show_sum(num1, num2):
result = num1 + num2
print(result)
Example 2:
def main():
show_interest(rate=0.01, periods=10, principal=10000.0)
def show_interest(principal, rate, periods):
interest = principal * rate * periods
print('The amount of interest is ’,
format(interest, ’.2f’),
sep=’’)
main()
8. Defining and calling a function
that returns a value.
▪ A value return function is a function
that returns a value to the component
that calls it.
▪ Python offers a library of ready-made
functions to perform various
operations (the term library function is
used).
▪ A function that returns a value
contains a return statement that
passes the return value to the
component that called it (example 2).
Example 1:
import random
def main():
number = random.randint(1, 10)
print(’ The number is’, number)
main()
Example 2:
def main():
first_age = int(input(’ Enter your age: ’))
second_age = int(input(’ Enter your age: ’))
total = sum(first_age, second_age)
print(’ Together you have’, total)
def sum(num1, num2):
result = num1 + num2
return result
main()
9. Source
▪ Gaddis T., Python dla zupełnie początkujących.
Wydanie IV. Helion 2019.
▪ Official source form Python community
▪ https://docs.python.org/3.11/tutorial/index.ht
ml
10. Thank you for your
attention! Please submit
any comments on the
software-engineers.org
PhD Artur Machura
arturmachura.info