[go: up one dir, main page]

0% found this document useful (0 votes)
35 views35 pages

Py Week 06 1

This document provides an overview of functions in Python. It defines what a function is, including that it is a block of reusable code that performs a single task. There are two types of functions: built-in functions provided by Python and user-defined functions created by developers. The document discusses function declaration syntax using the def keyword, how to call functions by name and pass arguments, and different argument types like positional, keyword, default, and variable arguments. It concludes by listing benefits of using functions like improved code reusability, readability and organization, and testability.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views35 pages

Py Week 06 1

This document provides an overview of functions in Python. It defines what a function is, including that it is a block of reusable code that performs a single task. There are two types of functions: built-in functions provided by Python and user-defined functions created by developers. The document discusses function declaration syntax using the def keyword, how to call functions by name and pass arguments, and different argument types like positional, keyword, default, and variable arguments. It concludes by listing benefits of using functions like improved code reusability, readability and organization, and testability.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 35

Python for Beginners

ෙනාහැක්කක් ෙනාමැත

Week -06 : Functions

www.heladevs.org
Where we are ??

Functions
What is Part 2 -
Intro
Computing Advanced
Programming concepts
Languages
Variables &
Data Types

Flow Control
Flow Control
Continuation

Loops

Functions

week-01 week-14
Previous This Week
Week
Python type system

● Statements
● What is a function
● While loop ● Types of functions
● for loop
● Function
● break statement
declaration
● continue statement

● Nested loops
● Calling functions

● Arguments

● Return of a
function

● Benefits of using
functions
What is a Function?
• A function is a block of organized, reusable code that is used to perform a
single, related action
• In other words, function can be treated as a subprogram, a small program
within a much larger program
• It is a sequence of statements which has a name. We can execute this
sequence at any given time by referring to this name
• Functions can also take one or more inputs and process them and produce
one or more outputs
• Functions provides better modularity for your application and a high degree
of code reusing
• They also increase the code readability
Real world Examples
• Displaying data
• Validating user inputs
• A calculation on a set of numbers (ie. average, max, min, std.dev)
• Conversion of units
• Send an email
• Performing a file operation
Functions vs Methods
• A method also refers to a
function which is a part of a
class
• We need an instance or object
of that class to call or use a
method
• But a function is not attached
to a particular class.
• Python classes, instances,
methods will be covered in a
separate session
Types of Functions
There are two types of functions in python.

• Built-in functions – The functions that are


provided by python itself. These are also
called predefined functions. print(),
range(), int(), float() are few
examples
• User-defined functions – The functions
that are created by developers according
to a specific requirement
Example for a User-defined Function
Example for Built-in functions

https://docs.python.org/3/library/functions.html
Python Function Declaration
• Function begins with “def” keyword followed by the name of the
function and parentheses – ()
• Next, we can define the parameters (inputs) as per the requirements
within these parentheses. This is optional
• Next, the colon indicates the end of the python function header (the
name and parameter list)
• The body of statements can contain a block of valid python code
• The return expression exits a function. This is also optional like input
parameters. But it is very useful when we want to pass back an
expression to the caller or return an output to the calling code
Python Function Declaration
Naming a Function
• Function names should use only letters, numbers, and underscores _
• Function names should not start with digits
• Function names should not use spaces or other special characters.
• If the function name consists of multiple words, it should use the snake_case
convention, which means using underscores (_) to separate words.
• Function names should follow the PEP 8 style guide.

PEP 8 Style Guide


Calling a Function
• To call a function, we can use the name
of the function with parentheses. If the
function accepts parameters, We can
also pass those parameters within the
parentheses ()
• We must be mindful about the
indentation of function body
• Indication of parameter types (explicit
types) is optional, but it is also a good
practice
• Indication of the function return type
is also optional, but it is encouraged to
have that for your python functions
Parameters vs Arguments
• A parameter is a variable which we use in the function definition.
• It is the handle that allow the code in the function to access the
argument for a particular invocation of the function
• An argument is a value we pass into the function as an input when we
invoke the function
• We can use different arguments each time when we invoke the
function to alter the work carried out by the function
Parameters vs Arguments
Function Arguments
• There are 4 types of arguments in python function definition
• Positional arguments
• Default arguments
• Keyword arguments
• Variable-length arguments (kwargs)
• Arbitrary positional arguments
• Arbitrary keyword arguments
Positional Arguments
• When the values are passed in the same order as of the parameters of
the function, we call them as positional arguments
Keyword Arguments
• Arguments can be passed into the functions in the form of
“kwarg=value”
• This way, values can be passed in any order as we wish them to be
• Keyword should match the function parameters defined in the function
Keyword Arguments
Default Arguments
• Default arguments are values
that are provided while
defining functions
• These provided values can be
overridden during function
invocation
• Default arguments should
follow non-default arguments
Default Arguments
Arbitrary Positional Arguments
• An asterisk (*) can be used
before a function parameter to
indicate an arbitrary positional
argument when defining a
function
• This is a collection (tuple) of
non-keyword arguments; the
number (zero or more) of
arguments varies based on the
function call
Arbitrary Keyword Arguments (kwargs)
• Double asterisk (**) can be
used before a function
parameter to indicate an
arbitrary keyword argument
when defining a function
• This is a collection (dictionary)
of keyword arguments; the
number (zero or more) of
arguments varies based on the
function call
Arbitrary Keyword Arguments (kwargs)
Return Statement in Python Function
• The function return statement is used to exit from a function and go
back to caller
• Optionally, it can be used to return data to the caller
• The return statement can consist a variable, constant or an expression
Benefits of Using Functions
• Code reusability – The same function can be used multiple number of
time in our program without rewriting the statements in the function
at each place we call the function from
Benefits of Using Functions
• Code readability / organization – Functions helps us to segregate our
code into smaller chunks so that our code is easily readable. We can
also keep similar functionalities together so that our program is more
organized. This will help us to reduce the cost of maintenance of our
program

• Testability of the code – Small groups of code is easier to debug, and


relatively smaller group of statements make it easier to write tests.
Unit testing is known practice in software development which
increase the quality of what our program produces.
Benefits of Using Functions
• Scalability of the code - A complex problem can be broken into
smaller pieces using functions and call them accordingly
Next Week – Week 07

Week 14

Week 08
• Functions part 2
Week 07 (Advanced
• Functions concepts)
ෙනාහැක්කක් ෙනාමැත

Want to build my muscles … ?


(PROGRAMMING Muscle )
Without a plan …..
Busy with my daily work,
No TIME…
Source : https://www.youtube.com/watch?v=kkeSegHJzc4
THANK YOU
ෙනාහැක්කක් ෙනාමැත.

You might also like