Professional Documents
Culture Documents
FUNCTIONS -9
Basimah Aljhne
11/05/2023
Click icon to add picture
1. An Introduction to Function in Python
2. User-defined Functions
3. Example
AN INTRODUCTION TO Click icon to add picture
FUNCTION IN PYTHON
1. What is function?
2. Why Using them?
3. Where do the functions come from?
What is Function?
4
What is Function?
Built-in functions
User-defined Functions
5
Why have them?
6
Program: Program:
Output:
def thing():
print('Hello’)
print ('Hello’)
print ('Fun’)
print ('Fun’)
Hello
Fun
print (‘python’) python
thing()
print (‘python’) Hello
print('Hello’)
thing() Fun
print ('Fun’)
thing() Hello
print('Hello’) Fun
print ('Fun’)
8
USER-DEFINED FUNCTIONS Click icon to add picture
1. Defining a Function in Python
2. Flow of Control With Functions
3. Rules for naming python function (identifier)
4. Python Function Parameters
5. Function Arguments
6. Python return Statement
7. Scope and Lifetime of Variables in Python
8. How to write a function
1. Defining a Function in Python
Function_Body.
execution.
function_name(parameter,parmater) 10
2. Flow of Control With Functions
Function
Statement
Statement
Var_Value Var_Name
result= area_square (2)
result
print(result)
Statement
Statement
11
2. Flow of Control With Functions
Function
Statement
Statement
Var_Value Var_Name
result= area_square (2)
result
print(result) side 2
Statement
Statement
12
2. Flow of Control With Functions
Function
Statement
Statement
Var_Value Var_Name
result= area_square (2)
result
print(result) side 2
Statement area 4
Statement
13
2. Flow of Control With Functions
Function
Statement
Statement 4
Var_Value Var_Name
result= area_square (2)
result 4
print(result) side 2
Statement area 4
Statement
14
3. Rules for naming python function (identifier)
15
3. Rules for naming python function (identifier)
19
Q4:what is the output?
20
Q5:What is the output of the following code? Explain why.
21
What is the output of the following code? Explain why.
22
What is the output of the following code? Explain why.
23
: What is the output of the following code? Explain why.
4. Python Function Parameters
A parameter is a variable , but there are two
important factors that make parameters different
and special:
They exist only inside functions in which they
have been defined, and the only place where
the parameter can be defined is a space
between a pair of parentheses in
the def statement; Don’t forget:
parameters live inside functions
Assigning a value to the parameter is done at (this is their natural environment)
the time of the function’s invocation, by arguments exist outside functions,
specifying the corresponding argument. and are carriers of values passed
to corresponding parameters.
26
4. Python Function Parameters
a=2 b=5
27
5. Function Arguments
• An argument is a value we pass into the function as its input when we call the function
• We use arguments, so we can direct the function to do different kinds of work when
we call it at different times
• We put the arguments in parenthesis after the name of the function
29
5. Function Parameters
30
1. Default Parameters in Python
Default value
Calling function in
different way
31
Value Symbol Name
User name
Basimah
32
2. Python Keyword Parameters
• With keyword Parameters in python, we can change the order of passing the
arguments without any consequences.
• You may not always know how many arguments you’ll get.
• In that case, you use an asterisk(*) before an argument name.
34
Q6: What is the output of the following snippet?
Q7: What is the output of the following snippet?
USER-DEFINED FUNCTIONS Click icon to add picture
1. Defining a Function in Python
2. Flow of Control With Functions
3. Rules for naming python function (identifier)
4. Python Function Parameters
5. Function Arguments
6. Python return Statement
7. Scope and Lifetime of Variables in Python
8. How to write a function
What is the output of the following code?
5
Error
Value Symbol Name
7
n 2
8
38
Write a function that takes the final scores of two soccer
teams as arguments and prints either who won the game
or whether the game was tied.
Refer to the teams as “Team1” and “Team2.”
What is my
arguments?
39
USER-DEFINED FUNCTIONS Click icon to add picture
1. Defining a Function in Python
2. Flow of Control With Functions
3. Rules for naming python function (identifier)
4. Python Function Parameters
5. Function Arguments
6. Python return Statement
7. Scope and Lifetime of Variables in Python
8. How to write a function
USER-DEFINED FUNCTIONS Click icon to add picture
Don’t forget:
1. Defining a Function in Python
2. Flow of Control With Functions
parameters live inside functions
3. Rules for naming python function (identifier) (this is their natural environment)
4. Python Function Parameters arguments exist outside functions,
5. Function Arguments and are carriers of values passed
6. Python return Statement to corresponding parameters.
7. Scope and Lifetime of Variables in Python
8. How to write a function
Types of Python Function Parameters
A value-returning function
A void function
Multiple returns in a function
44
6. Python return Statement
A value-returning function
45
6. Python return Statement
46
6. Python return Statement
A void function
• Simply executes the statements it contains and then it returns a None
value
47
python return Statement
49
python return Statement
• It is the last statement executed by a function.
print(func1())
50
python return Statement
• It is the last statement executed by a function.
print(func1())
51
6. Python return Statement
52
0
53
0
54
55
USER-DEFINED FUNCTIONS Click icon to add picture
1. Defining a Function in Python
2. Flow of Control With Functions
3. Rules for naming python function (identifier)
4. Python Function Parameters
5. Function Arguments
6. Python return Statement
7. Scope and Lifetime of Variables in Python
8. How to write a function
If return statement is not used inside the function, the
function will return:
0
None object
Random integer
57
Write a Function that takes as input unknown integers and
returns the average of the values.
What is my arguments?
How many return should I
have?
58
Write a Function that takes as input unknown integers and
returns the average of the values.
59
Write a Function that takes as input two words and return
True if both start with the same character, and False if
not.
Note that:
Case distinction is ignored
What is my arguments?
How many return should I
have?
60
61
8. Scope and Lifetime of Variables in Python
62
8. Scope and Lifetime of Variables in Python
1. Scope
The scope of a variable refers to the places that you can see or access a variable.
A variable may have local or global scope.
Local Scope
Global Scope
63
8. Scope and Lifetime of Variables in Python
Local Scope
A variable that’s declared inside a function has a local scope.
In other words, it is local to that function.
If you then try to access the variable x outside the function, you cannot.
I am a local variable
scope
No one outside this
function can see me
64
8. Scope and Lifetime of Variables in Python
Global Scope
• When you declare a variable outside python functions, or anything else, it has global
scope.
• It means that it is visible everywhere within the program.
Hi, I am visible
everywhere within
the program.
65
8. Scope and Lifetime of Variables in Python
Global Scope
• When you declare a variable outside python functions, or anything else, it has global
scope.
• It means that it is visible everywhere within the program.
• However, you can’t change its value from inside a local scope
(here, inside a function).
you can see me but
you can’t change me
from inside local
scope
66
8. Scope and Lifetime of Variables in Python
• In Python, global keyword allows you to modify the variable outside of the current
scope.
• It is used to create a global variable and make changes to the variable in a local
context.
X=7
X=2
67
Rules of global Keyword
68
8. Scope and Lifetime of Variables in Python
2. Lifetime
• The lifespan of a variable local to a function is from the time it is created in the
function’s run, until the function ends (and the memory allocated to the variable is
released) It’s the same for parameters to that function also.
69
8. Scope and Lifetime of Variables in Python
2. Lifetime
• The lifespan of a variable local to a function is from the time it is created in the
function’s run, until the function ends (and the memory allocated to the variable is
released) It’s the same for parameters to that function also.
x=cs111
70
8. Scope and Lifetime of Variables in Python
2. Lifetime
• The lifespan of a variable local to a function is from the time it is created in the
function’s run, until the function ends (and the memory allocated to the variable is
released) It’s the same for parameters to that function also.
x=cs111
71
8. Scope and Lifetime of Variables in Python
2. Lifetime
• The lifespan of a variable local to a function is from the time it is created in the
function’s run, until the function ends (and the memory allocated to the variable is
released) It’s the same for parameters to that function also.
X=“defult”
72
8. Scope and Lifetime of Variables in Python
2. Lifetime
• The lifespan of a variable local to a function is from the time it is created in the
function’s run, until the function ends (and the memory allocated to the variable is
released) It’s the same for parameters to that function also.
73
8. Scope and Lifetime of Variables in Python
2. Lifetime
• The lifespan of a variable local to a function is from the time it is created in the
function’s run, until the function ends (and the memory allocated to the variable is
released) It’s the same for parameters to that function also.
• If a function with locals is called several times, then those variables are created and
destroyed, created and destroyed, over and over for each call, their lifespan is
intermittent
74
8. Scope and Lifetime of Variables in Python
2. Lifetime
• The lifespan of a variable local to a function is from the time it is created in the
function’s run, until the function ends (and the memory allocated to the variable is
released) It’s the same for parameters to that function also.
• If a function with locals is called several times, then those variables are created and
destroyed, created and destroyed, over and over for each call, their lifespan is
intermittent
• The lifespan of a global variable is from the time its definition is encountered during
the execution of the whole program, until the program is finished 75
USER-DEFINED FUNCTIONS Click icon to add picture
1. Defining a Function in Python
2. Flow of Control With Functions
3. Rules for naming python function (identifier)
4. Python Function Parameters
5. Function Arguments
6. Python return Statement
7. Scope and Lifetime of Variables in Python
8. How to write a function
1
77
USER-DEFINED FUNCTIONS Click icon to add picture
1. Defining a Function in Python
2. Flow of Control With Functions
3. Rules for naming python function (identifier)
4. Python Function Parameters
5. Function Arguments
6. Python return Statement
7. Scope and Lifetime of Variables in Python
8. How to write a function
9. How to write a function
• Does one thing. If it does too many things, it should be broken down into
multiple functions (refactored)
• Readable. How often should we say this? If you write it, it should be
readable
• Reusable .If it does one thing well, then when a similar situation (in another
program) occurs, use it there as well.
• Complete. A function should check for all the cases where it might be
invoked. Check for potential errors.
• Not too long. Kind of synonymous with do one thing. Use it as a measure of
doing too much.
79
Reminder, rules so far
1. Think before you program!
2. A program is a human-readable essay on problem solving that also happens to
execute on a computer.
3. The best way to improve your programming and problem solving skills is to
practice!
4. Test your code, often and thoroughly
5. If it was hard to write, it is probably hard to read. Add a comment.
6. A function should do one thing.
80
USER-DEFINED FUNCTIONS Click icon to add picture
1. Defining a Function in Python
2. Flow of Control With Functions
3. Rules for naming python function (identifier)
4. Python Function Parameters
5. Function Arguments
6. Python return Statement
7. Scope and Lifetime of Variables in Python
8. How to write a function
What is the output of the following code? Explain why.
82
What is the output of the following code? Explain why.
83
What is the output of the following code? Explain why.
84
What is the output of the following code? Explain why.
85
What is the output of the following code? Explain why.
86
Important to do at home :
Read chapter 6
87
Click icon to add picture
EXAMPLE
Exercises
Write a Function that takes as input an integer value and returns the sum
of all integers less than that value.
For example,
if the input is 6, the output is 5+4+3+2+1 = 15.
If the input is negative, the output should be -1.
What is my arguments?
How many return should I have?
89
Exercises
Write a Python function that take from the user the range limit and
then take test number and check whether a number is in a given
range
90
Exercises
Write a Python function that take from the user the range limit.
if start less than end the swab between them and then print the all
numbers between them
91
1) Write a Python function that take list
then return a copy from the list.
3) Write a Python program to print a specified list after removing the 0th,
4th and 5th elements.