SlideShare a Scribd company logo
1 of 11
Download to read offline
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

Similar to python_function.pdf

The Ring programming language version 1.5.2 book - Part 20 of 181
The Ring programming language version 1.5.2 book - Part 20 of 181The Ring programming language version 1.5.2 book - Part 20 of 181
The Ring programming language version 1.5.2 book - Part 20 of 181Mahmoud Samir Fayed
 
The Ring programming language version 1.8 book - Part 26 of 202
The Ring programming language version 1.8 book - Part 26 of 202The Ring programming language version 1.8 book - Part 26 of 202
The Ring programming language version 1.8 book - Part 26 of 202Mahmoud Samir Fayed
 
Chapter_1.__Functions_in_C++[1].pdf
Chapter_1.__Functions_in_C++[1].pdfChapter_1.__Functions_in_C++[1].pdf
Chapter_1.__Functions_in_C++[1].pdfTeshaleSiyum
 
Chapter 1. Functions in C++.pdf
Chapter 1.  Functions in C++.pdfChapter 1.  Functions in C++.pdf
Chapter 1. Functions in C++.pdfTeshaleSiyum
 
The Ring programming language version 1.5 book - Part 4 of 31
The Ring programming language version 1.5 book - Part 4 of 31The Ring programming language version 1.5 book - Part 4 of 31
The Ring programming language version 1.5 book - Part 4 of 31Mahmoud Samir Fayed
 
The Ring programming language version 1.5.4 book - Part 21 of 185
The Ring programming language version 1.5.4 book - Part 21 of 185The Ring programming language version 1.5.4 book - Part 21 of 185
The Ring programming language version 1.5.4 book - Part 21 of 185Mahmoud Samir Fayed
 
The Ring programming language version 1.6 book - Part 23 of 189
The Ring programming language version 1.6 book - Part 23 of 189The Ring programming language version 1.6 book - Part 23 of 189
The Ring programming language version 1.6 book - Part 23 of 189Mahmoud Samir Fayed
 
Lecture 1_Functions in C.pptx
Lecture 1_Functions in C.pptxLecture 1_Functions in C.pptx
Lecture 1_Functions in C.pptxKhurramKhan173
 
Programming Fundamentals Functions in C and types
Programming Fundamentals  Functions in C  and typesProgramming Fundamentals  Functions in C  and types
Programming Fundamentals Functions in C and typesimtiazalijoono
 
Learning C++ - Functions in C++ 3
Learning C++ - Functions  in C++ 3Learning C++ - Functions  in C++ 3
Learning C++ - Functions in C++ 3Ali Aminian
 
The Ring programming language version 1.7 book - Part 24 of 196
The Ring programming language version 1.7 book - Part 24 of 196The Ring programming language version 1.7 book - Part 24 of 196
The Ring programming language version 1.7 book - Part 24 of 196Mahmoud Samir Fayed
 
16717 functions in C++
16717 functions in C++16717 functions in C++
16717 functions in C++LPU
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAAiman Hud
 
Python programming - Functions and list and tuples
Python programming - Functions and list and tuplesPython programming - Functions and list and tuples
Python programming - Functions and list and tuplesMalligaarjunanN
 

Similar to python_function.pdf (20)

The Ring programming language version 1.5.2 book - Part 20 of 181
The Ring programming language version 1.5.2 book - Part 20 of 181The Ring programming language version 1.5.2 book - Part 20 of 181
The Ring programming language version 1.5.2 book - Part 20 of 181
 
The Ring programming language version 1.8 book - Part 26 of 202
The Ring programming language version 1.8 book - Part 26 of 202The Ring programming language version 1.8 book - Part 26 of 202
The Ring programming language version 1.8 book - Part 26 of 202
 
Chapter_1.__Functions_in_C++[1].pdf
Chapter_1.__Functions_in_C++[1].pdfChapter_1.__Functions_in_C++[1].pdf
Chapter_1.__Functions_in_C++[1].pdf
 
Chapter 1. Functions in C++.pdf
Chapter 1.  Functions in C++.pdfChapter 1.  Functions in C++.pdf
Chapter 1. Functions in C++.pdf
 
The Ring programming language version 1.5 book - Part 4 of 31
The Ring programming language version 1.5 book - Part 4 of 31The Ring programming language version 1.5 book - Part 4 of 31
The Ring programming language version 1.5 book - Part 4 of 31
 
Functions
Functions Functions
Functions
 
Pro
ProPro
Pro
 
The Ring programming language version 1.5.4 book - Part 21 of 185
The Ring programming language version 1.5.4 book - Part 21 of 185The Ring programming language version 1.5.4 book - Part 21 of 185
The Ring programming language version 1.5.4 book - Part 21 of 185
 
The Ring programming language version 1.6 book - Part 23 of 189
The Ring programming language version 1.6 book - Part 23 of 189The Ring programming language version 1.6 book - Part 23 of 189
The Ring programming language version 1.6 book - Part 23 of 189
 
Lecture 1_Functions in C.pptx
Lecture 1_Functions in C.pptxLecture 1_Functions in C.pptx
Lecture 1_Functions in C.pptx
 
Function
FunctionFunction
Function
 
Programming Fundamentals Functions in C and types
Programming Fundamentals  Functions in C  and typesProgramming Fundamentals  Functions in C  and types
Programming Fundamentals Functions in C and types
 
Learning C++ - Functions in C++ 3
Learning C++ - Functions  in C++ 3Learning C++ - Functions  in C++ 3
Learning C++ - Functions in C++ 3
 
The Ring programming language version 1.7 book - Part 24 of 196
The Ring programming language version 1.7 book - Part 24 of 196The Ring programming language version 1.7 book - Part 24 of 196
The Ring programming language version 1.7 book - Part 24 of 196
 
16717 functions in C++
16717 functions in C++16717 functions in C++
16717 functions in C++
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIA
 
Python programming - Functions and list and tuples
Python programming - Functions and list and tuplesPython programming - Functions and list and tuples
Python programming - Functions and list and tuples
 
Function
FunctionFunction
Function
 
functions- best.pdf
functions- best.pdffunctions- best.pdf
functions- best.pdf
 
Functions
FunctionsFunctions
Functions
 

More from University of Economics in Katowice

Initiation the Java web application project in the Google App Engine
Initiation the Java web application project in the Google App EngineInitiation the Java web application project in the Google App Engine
Initiation the Java web application project in the Google App EngineUniversity of Economics in Katowice
 

More from University of Economics in Katowice (20)

python_p4_v2.pdf
python_p4_v2.pdfpython_p4_v2.pdf
python_p4_v2.pdf
 
python_p3.pdf
python_p3.pdfpython_p3.pdf
python_p3.pdf
 
python_p2v2_publikacja.pdf
python_p2v2_publikacja.pdfpython_p2v2_publikacja.pdf
python_p2v2_publikacja.pdf
 
python_p1.pdf
python_p1.pdfpython_p1.pdf
python_p1.pdf
 
TechnologyStack_basicsv2.pdf
TechnologyStack_basicsv2.pdfTechnologyStack_basicsv2.pdf
TechnologyStack_basicsv2.pdf
 
InitiateAEv2.pdf
InitiateAEv2.pdfInitiateAEv2.pdf
InitiateAEv2.pdf
 
barplotv4.pdf
barplotv4.pdfbarplotv4.pdf
barplotv4.pdf
 
Initiation the Java web application project in the Google App Engine
Initiation the Java web application project in the Google App EngineInitiation the Java web application project in the Google App Engine
Initiation the Java web application project in the Google App Engine
 
ie.pdf
ie.pdfie.pdf
ie.pdf
 
puś.ppt
puś.pptpuś.ppt
puś.ppt
 
swd.pdf
swd.pdfswd.pdf
swd.pdf
 
EARv3.pdf
EARv3.pdfEARv3.pdf
EARv3.pdf
 
Projektowanie i implementacja usług sieciowych
Projektowanie i implementacja usług sieciowychProjektowanie i implementacja usług sieciowych
Projektowanie i implementacja usług sieciowych
 
Angular10302021
Angular10302021Angular10302021
Angular10302021
 
Środowisko PWA
Środowisko PWAŚrodowisko PWA
Środowisko PWA
 
Kolo REST
Kolo RESTKolo REST
Kolo REST
 
Inicjacja wg OpenUP
Inicjacja wg OpenUPInicjacja wg OpenUP
Inicjacja wg OpenUP
 
Dyscyplina zarządzania projektami wg OpenUP
Dyscyplina zarządzania projektami wg OpenUPDyscyplina zarządzania projektami wg OpenUP
Dyscyplina zarządzania projektami wg OpenUP
 
Atrybut zgodności
Atrybut zgodnościAtrybut zgodności
Atrybut zgodności
 
Wstęp do dyscypliny wymagań w projektach IT
Wstęp do dyscypliny wymagań w projektach ITWstęp do dyscypliny wymagań w projektach IT
Wstęp do dyscypliny wymagań w projektach IT
 

Recently uploaded

Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Jisc
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxmarlenawright1
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfNirmal Dwivedi
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxAmanpreet Kaur
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptxMaritesTamaniVerdade
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...Poonam Aher Patil
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Association for Project Management
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxJisc
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.christianmathematics
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.pptRamjanShidvankar
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxPooja Bhuva
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...pradhanghanshyam7136
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jisc
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxJisc
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxRamakrishna Reddy Bijjam
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfSherif Taha
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - Englishneillewis46
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxDr. Ravikiran H M Gowda
 

Recently uploaded (20)

Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
Spatium Project Simulation student brief
Spatium Project Simulation student briefSpatium Project Simulation student brief
Spatium Project Simulation student brief
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptx
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptx
 

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