Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
14 views

B12 04 Python Functions

Uploaded by

nthang0987
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

B12 04 Python Functions

Uploaded by

nthang0987
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 17

Python Functions

Lê Ngọc Hiếu
lnhieu@hcmute.edu.vn
1. Definition of Functions

2. Python Build-in Functions


Lecture
Contents 3. User-defined Functions

4. Exercises

2
Definition of Functions
• A function is a block of code which only runs when it is
called.
• Functions perform a predefined task and can be called upon
in any program, as per requirement.
• You can pass data, known as parameters, into a function.
• A function can return data as a result.
• Two types of functions:
• Build-in functions: pre-defined in Python, present for use.
• User-defined functions: defined by developers in programs.

3
Python Build-in Functions

4
Several Build-in Functions in Python
Functio Description Functio Description
n n
abs() Returns the absolute print() Prints to the standard
value of a number output device
ascii() Returns a readable input() Allowing user input
version of an object. int() Returns an integer number
Replaces none-ascii float() Returns a float number
characters with escape max() Returns the largest item in
character an iterable
bin() Returns the binary version min() Returns the smallest item in
of a number an iterable
id() Returns the id of an object pow() Returns the value of
type() Returns the type of an round() Rounds a numbers
object sorted() Returns a sorted list
range() Returns a sequence of sum() Sums the items of an
numbers, starting from 0 iterator 5
User-defined Functions

6
Defining a Function
• A function has two main parts: a function definition and
body.
• Syntax of defining a function:
• Use def keyword
• Docstring and return are optional.
def
function_name(parameters):
"""docstring"""
statement1
statement2
...
...
return [expr]
Calling a Function
• To use a function, you need to call it.
• A function call instructs Python to execute the code inside
the function.
• To call a function, write the name of the function, followed
by the information that the function needs in parentheses.
• Examples:

8
Function Arguments
• Information can be passed into functions as arguments.
• Arguments are specified after the function name, inside the
parentheses.
• Functions can have many arguments which are separated
by commas.

9
Parameters or Arguments?
• The terms parameter and argument can be used for the
same thing: information that are passed into a function.
• From a function's perspective:
• A parameter is the variable listed inside the parentheses in
the function definition.
• An argument is the value that is sent to the function when it
is called.

In the example:
• What are parameters?
• What are arguments?

10
Keyword Arguments
• Arguments can be sent with the key = value syntax.
• This way the order of the arguments does not matter.
• Example:

11
Default Parameter Value
• A function can have default parameter values.
• If the function is called without arguments, it uses the
default values.
• Example:

12
Arbitrary Arguments
• Add a * before the parameter name in the function
definition in case the number of arguments is not known
beforehand.

This example works.

13
Arbitrary Keyword Arguments
• If the number of keyword arguments is unknown, add a
double ** before the parameter name.

14
Exercises

15
1. Write a Python function to find the Max of three numbers.
Function name and parameters: max3(number1, number2, number3)
2. Write a function to compute the perimeter and area of a triangle given
side lengths of the triangle.
Function name and parameters : triangle(side1, side2, side3)
3. Write a Python function to calculate the factorial of a number (a non-
negative integer). The function accepts the number as an argument.
Function name and parameters: factorial(n)
4. Write a Python function that takes a number as a parameter and check
the number is prime or not.
Function name and parameters: isprime(n)
5. Write a Python function that takes a number as a parameter and check
the number is a perfect number.
Function name and parameters: isperfect(n)
6. Implement all exercises in the previous lessons as functions.
17

You might also like