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

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 181
Mahmoud 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 202
Mahmoud Samir Fayed
 
Chapter 1. Functions in C++.pdf
Chapter 1.  Functions in C++.pdfChapter 1.  Functions in C++.pdf
Chapter 1. Functions in C++.pdf
TeshaleSiyum
 
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
TeshaleSiyum
 
Funtions of c programming. the functions of c helps to clarify all the tops
Funtions of c programming. the functions of c helps to clarify all the topsFuntions of c programming. the functions of c helps to clarify all the tops
Funtions of c programming. the functions of c helps to clarify all the tops
sameermhr345
 
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
Mahmoud Samir Fayed
 
Functions
Functions Functions
Functions
Dr.Subha Krishna
 
Pro
ProPro
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
Mahmoud 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 189
Mahmoud Samir Fayed
 
Lecture 1_Functions in C.pptx
Lecture 1_Functions in C.pptxLecture 1_Functions in C.pptx
Lecture 1_Functions in C.pptx
KhurramKhan173
 
Function
FunctionFunction
Function
yash patel
 
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
imtiazalijoono
 
Learning C++ - Functions in C++ 3
Learning C++ - Functions  in C++ 3Learning C++ - Functions  in C++ 3
Learning C++ - Functions in C++ 3
Ali 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 196
Mahmoud 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 MALAYSIA
Aiman 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 tuples
MalligaarjunanN
 
Function
FunctionFunction
Function
Saniati
 
functions- best.pdf
functions- best.pdffunctions- best.pdf
functions- best.pdf
MikialeTesfamariam
 

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++.pdf
Chapter 1.  Functions in C++.pdfChapter 1.  Functions in C++.pdf
Chapter 1. Functions in C++.pdf
 
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
 
Funtions of c programming. the functions of c helps to clarify all the tops
Funtions of c programming. the functions of c helps to clarify all the topsFuntions of c programming. the functions of c helps to clarify all the tops
Funtions of c programming. the functions of c helps to clarify all the tops
 
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
 

More from University of Economics in Katowice

python_p4_v2.pdf
python_p4_v2.pdfpython_p4_v2.pdf
python_p3.pdf
python_p3.pdfpython_p3.pdf
python_p2v2_publikacja.pdf
python_p2v2_publikacja.pdfpython_p2v2_publikacja.pdf
python_p2v2_publikacja.pdf
University of Economics in Katowice
 
python_p1.pdf
python_p1.pdfpython_p1.pdf
TechnologyStack_basicsv2.pdf
TechnologyStack_basicsv2.pdfTechnologyStack_basicsv2.pdf
TechnologyStack_basicsv2.pdf
University of Economics in Katowice
 
InitiateAEv2.pdf
InitiateAEv2.pdfInitiateAEv2.pdf
barplotv4.pdf
barplotv4.pdfbarplotv4.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
University of Economics in Katowice
 
ie.pdf
ie.pdfie.pdf
puś.ppt
puś.pptpuś.ppt
swd.pdf
swd.pdfswd.pdf
EARv3.pdf
EARv3.pdfEARv3.pdf
Projektowanie i implementacja usług sieciowych
Projektowanie i implementacja usług sieciowychProjektowanie i implementacja usług sieciowych
Projektowanie i implementacja usług sieciowych
University of Economics in Katowice
 
Angular10302021
Angular10302021Angular10302021
Środowisko PWA
Środowisko PWAŚrodowisko PWA
Kolo REST
Kolo RESTKolo REST
Inicjacja wg OpenUP
Inicjacja wg OpenUPInicjacja 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
University of Economics in Katowice
 
Atrybut zgodności
Atrybut zgodnościAtrybut 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
University 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

What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
GeorgeMilliken2
 
BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...
BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...
BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...
Nguyen Thanh Tu Collection
 
Mule event processing models | MuleSoft Mysore Meetup #47
Mule event processing models | MuleSoft Mysore Meetup #47Mule event processing models | MuleSoft Mysore Meetup #47
Mule event processing models | MuleSoft Mysore Meetup #47
MysoreMuleSoftMeetup
 
Wound healing PPT
Wound healing PPTWound healing PPT
Wound healing PPT
Jyoti Chand
 
Nutrition Inc FY 2024, 4 - Hour Training
Nutrition Inc FY 2024, 4 - Hour TrainingNutrition Inc FY 2024, 4 - Hour Training
Nutrition Inc FY 2024, 4 - Hour Training
melliereed
 
Lifelines of National Economy chapter for Class 10 STUDY MATERIAL PDF
Lifelines of National Economy chapter for Class 10 STUDY MATERIAL PDFLifelines of National Economy chapter for Class 10 STUDY MATERIAL PDF
Lifelines of National Economy chapter for Class 10 STUDY MATERIAL PDF
Vivekanand Anglo Vedic Academy
 
UGC NET Exam Paper 1- Unit 1:Teaching Aptitude
UGC NET Exam Paper 1- Unit 1:Teaching AptitudeUGC NET Exam Paper 1- Unit 1:Teaching Aptitude
UGC NET Exam Paper 1- Unit 1:Teaching Aptitude
S. Raj Kumar
 
Gender and Mental Health - Counselling and Family Therapy Applications and In...
Gender and Mental Health - Counselling and Family Therapy Applications and In...Gender and Mental Health - Counselling and Family Therapy Applications and In...
Gender and Mental Health - Counselling and Family Therapy Applications and In...
PsychoTech Services
 
مصحف القراءات العشر أعد أحرف الخلاف سمير بسيوني.pdf
مصحف القراءات العشر   أعد أحرف الخلاف سمير بسيوني.pdfمصحف القراءات العشر   أعد أحرف الخلاف سمير بسيوني.pdf
مصحف القراءات العشر أعد أحرف الخلاف سمير بسيوني.pdf
سمير بسيوني
 
Standardized tool for Intelligence test.
Standardized tool for Intelligence test.Standardized tool for Intelligence test.
Standardized tool for Intelligence test.
deepaannamalai16
 
Jemison, MacLaughlin, and Majumder "Broadening Pathways for Editors and Authors"
Jemison, MacLaughlin, and Majumder "Broadening Pathways for Editors and Authors"Jemison, MacLaughlin, and Majumder "Broadening Pathways for Editors and Authors"
Jemison, MacLaughlin, and Majumder "Broadening Pathways for Editors and Authors"
National Information Standards Organization (NISO)
 
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem studentsRHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
Himanshu Rai
 
BBR 2024 Summer Sessions Interview Training
BBR  2024 Summer Sessions Interview TrainingBBR  2024 Summer Sessions Interview Training
BBR 2024 Summer Sessions Interview Training
Katrina Pritchard
 
Level 3 NCEA - NZ: A Nation In the Making 1872 - 1900 SML.ppt
Level 3 NCEA - NZ: A  Nation In the Making 1872 - 1900 SML.pptLevel 3 NCEA - NZ: A  Nation In the Making 1872 - 1900 SML.ppt
Level 3 NCEA - NZ: A Nation In the Making 1872 - 1900 SML.ppt
Henry Hollis
 
Chapter wise All Notes of First year Basic Civil Engineering.pptx
Chapter wise All Notes of First year Basic Civil Engineering.pptxChapter wise All Notes of First year Basic Civil Engineering.pptx
Chapter wise All Notes of First year Basic Civil Engineering.pptx
Denish Jangid
 
Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...
Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...
Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...
imrankhan141184
 
How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17
Celine George
 
math operations ued in python and all used
math operations ued in python and all usedmath operations ued in python and all used
math operations ued in python and all used
ssuser13ffe4
 
Stack Memory Organization of 8086 Microprocessor
Stack Memory Organization of 8086 MicroprocessorStack Memory Organization of 8086 Microprocessor
Stack Memory Organization of 8086 Microprocessor
JomonJoseph58
 
spot a liar (Haiqa 146).pptx Technical writhing and presentation skills
spot a liar (Haiqa 146).pptx Technical writhing and presentation skillsspot a liar (Haiqa 146).pptx Technical writhing and presentation skills
spot a liar (Haiqa 146).pptx Technical writhing and presentation skills
haiqairshad
 

Recently uploaded (20)

What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
 
BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...
BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...
BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...
 
Mule event processing models | MuleSoft Mysore Meetup #47
Mule event processing models | MuleSoft Mysore Meetup #47Mule event processing models | MuleSoft Mysore Meetup #47
Mule event processing models | MuleSoft Mysore Meetup #47
 
Wound healing PPT
Wound healing PPTWound healing PPT
Wound healing PPT
 
Nutrition Inc FY 2024, 4 - Hour Training
Nutrition Inc FY 2024, 4 - Hour TrainingNutrition Inc FY 2024, 4 - Hour Training
Nutrition Inc FY 2024, 4 - Hour Training
 
Lifelines of National Economy chapter for Class 10 STUDY MATERIAL PDF
Lifelines of National Economy chapter for Class 10 STUDY MATERIAL PDFLifelines of National Economy chapter for Class 10 STUDY MATERIAL PDF
Lifelines of National Economy chapter for Class 10 STUDY MATERIAL PDF
 
UGC NET Exam Paper 1- Unit 1:Teaching Aptitude
UGC NET Exam Paper 1- Unit 1:Teaching AptitudeUGC NET Exam Paper 1- Unit 1:Teaching Aptitude
UGC NET Exam Paper 1- Unit 1:Teaching Aptitude
 
Gender and Mental Health - Counselling and Family Therapy Applications and In...
Gender and Mental Health - Counselling and Family Therapy Applications and In...Gender and Mental Health - Counselling and Family Therapy Applications and In...
Gender and Mental Health - Counselling and Family Therapy Applications and In...
 
مصحف القراءات العشر أعد أحرف الخلاف سمير بسيوني.pdf
مصحف القراءات العشر   أعد أحرف الخلاف سمير بسيوني.pdfمصحف القراءات العشر   أعد أحرف الخلاف سمير بسيوني.pdf
مصحف القراءات العشر أعد أحرف الخلاف سمير بسيوني.pdf
 
Standardized tool for Intelligence test.
Standardized tool for Intelligence test.Standardized tool for Intelligence test.
Standardized tool for Intelligence test.
 
Jemison, MacLaughlin, and Majumder "Broadening Pathways for Editors and Authors"
Jemison, MacLaughlin, and Majumder "Broadening Pathways for Editors and Authors"Jemison, MacLaughlin, and Majumder "Broadening Pathways for Editors and Authors"
Jemison, MacLaughlin, and Majumder "Broadening Pathways for Editors and Authors"
 
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem studentsRHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
 
BBR 2024 Summer Sessions Interview Training
BBR  2024 Summer Sessions Interview TrainingBBR  2024 Summer Sessions Interview Training
BBR 2024 Summer Sessions Interview Training
 
Level 3 NCEA - NZ: A Nation In the Making 1872 - 1900 SML.ppt
Level 3 NCEA - NZ: A  Nation In the Making 1872 - 1900 SML.pptLevel 3 NCEA - NZ: A  Nation In the Making 1872 - 1900 SML.ppt
Level 3 NCEA - NZ: A Nation In the Making 1872 - 1900 SML.ppt
 
Chapter wise All Notes of First year Basic Civil Engineering.pptx
Chapter wise All Notes of First year Basic Civil Engineering.pptxChapter wise All Notes of First year Basic Civil Engineering.pptx
Chapter wise All Notes of First year Basic Civil Engineering.pptx
 
Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...
Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...
Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...
 
How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17
 
math operations ued in python and all used
math operations ued in python and all usedmath operations ued in python and all used
math operations ued in python and all used
 
Stack Memory Organization of 8086 Microprocessor
Stack Memory Organization of 8086 MicroprocessorStack Memory Organization of 8086 Microprocessor
Stack Memory Organization of 8086 Microprocessor
 
spot a liar (Haiqa 146).pptx Technical writhing and presentation skills
spot a liar (Haiqa 146).pptx Technical writhing and presentation skillsspot a liar (Haiqa 146).pptx Technical writhing and presentation skills
spot a liar (Haiqa 146).pptx Technical writhing and presentation skills
 

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