SlideShare a Scribd company logo
Beginnings of programming in Python
Functions
Artur Machura
16.06.2023, Katowice
Abstract
▪ Introduction to functions
▪ Defining and calling functions (non-
returning and returning values)
▪ Local and global variables/constants
▪ Forwarding function arguments
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
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()
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()
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()
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()
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()
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
Thank you for your
attention! Please submit
any comments on the
software-engineers.org
PhD Artur Machura
arturmachura.info
python_function.pdf

More Related Content

PDF
PPTX
04. WORKING WITH FUNCTIONS-2 (1).pptx
PDF
functions- best.pdf
PDF
functions notes.pdf python functions and opp
PPTX
FUNCTIONS.pptx
PDF
3-Python Functions.pdf in simple.........
PPTX
JNTUK python programming python unit 3.pptx
PPTX
CHAPTER 01 FUNCTION in python class 12th.pptx
04. WORKING WITH FUNCTIONS-2 (1).pptx
functions- best.pdf
functions notes.pdf python functions and opp
FUNCTIONS.pptx
3-Python Functions.pdf in simple.........
JNTUK python programming python unit 3.pptx
CHAPTER 01 FUNCTION in python class 12th.pptx

Similar to python_function.pdf (20)

PDF
Functions2.pdf
PPTX
functions new.pptx
PPTX
cbse class 12 Python Functions2 for class 12 .pptx
PPTX
functioninpython-1.pptx
PPTX
Python Functions.pptx
PDF
Python_Functions.pdf
PPTX
UNIT 3 python.pptx
PPTX
Working with functions.pptx. Hb.
PPTX
Lecture 5 Functions all about business categories
PPT
Python programming variables and comment
PDF
Functions2.pdf
PPT
Powerpoint presentation for Python Functions
PPTX
Working with functions the minumum ppt.pptx
PPTX
Understanding Python Programming Language -Functions
PPTX
Functions and Modules.pptx
PPT
Py-Slides-3 difficultpythoncoursefforbeginners.ppt
PPTX
UNIT-02-pythonfunctions python function using detehdjsjehhdjejdhdjdjdjddjdhdhhd
PDF
Functions in Python.pdfnsjiwshkwijjahuwjwjw
PPTX
2_3 Functions 5d.pptx2_3 Functions 5d.pptx
PPT
python slides introduction interrupt.ppt
Functions2.pdf
functions new.pptx
cbse class 12 Python Functions2 for class 12 .pptx
functioninpython-1.pptx
Python Functions.pptx
Python_Functions.pdf
UNIT 3 python.pptx
Working with functions.pptx. Hb.
Lecture 5 Functions all about business categories
Python programming variables and comment
Functions2.pdf
Powerpoint presentation for Python Functions
Working with functions the minumum ppt.pptx
Understanding Python Programming Language -Functions
Functions and Modules.pptx
Py-Slides-3 difficultpythoncoursefforbeginners.ppt
UNIT-02-pythonfunctions python function using detehdjsjehhdjejdhdjdjdjddjdhdhhd
Functions in Python.pdfnsjiwshkwijjahuwjwjw
2_3 Functions 5d.pptx2_3 Functions 5d.pptx
python slides introduction interrupt.ppt

More from University of Economics in Katowice (20)

PDF
python_p2v2_publikacja.pdf
PDF
TechnologyStack_basicsv2.pdf
PDF
Initiation the Java web application project in the Google App Engine
PDF
Projektowanie i implementacja usług sieciowych
PDF
PDF
Dyscyplina zarządzania projektami wg OpenUP
PDF
Wstęp do dyscypliny wymagań w projektach IT
python_p2v2_publikacja.pdf
TechnologyStack_basicsv2.pdf
Initiation the Java web application project in the Google App Engine
Projektowanie i implementacja usług sieciowych
Dyscyplina zarządzania projektami wg OpenUP
Wstęp do dyscypliny wymagań w projektach IT

Recently uploaded (20)

PPTX
Cell Structure & Organelles in detailed.
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PPTX
Cell Types and Its function , kingdom of life
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
Pre independence Education in Inndia.pdf
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
Computing-Curriculum for Schools in Ghana
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
VCE English Exam - Section C Student Revision Booklet
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PPTX
Pharma ospi slides which help in ospi learning
Cell Structure & Organelles in detailed.
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
Cell Types and Its function , kingdom of life
Supply Chain Operations Speaking Notes -ICLT Program
STATICS OF THE RIGID BODIES Hibbelers.pdf
Pre independence Education in Inndia.pdf
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
Abdominal Access Techniques with Prof. Dr. R K Mishra
2.FourierTransform-ShortQuestionswithAnswers.pdf
FourierSeries-QuestionsWithAnswers(Part-A).pdf
human mycosis Human fungal infections are called human mycosis..pptx
Computing-Curriculum for Schools in Ghana
Final Presentation General Medicine 03-08-2024.pptx
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Pharmacology of Heart Failure /Pharmacotherapy of CHF
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
VCE English Exam - Section C Student Revision Booklet
Renaissance Architecture: A Journey from Faith to Humanism
Pharma ospi slides which help in ospi learning

python_function.pdf

  • 1. Beginnings of programming in Python Functions Artur Machura 16.06.2023, Katowice
  • 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