0% found this document useful (0 votes)
2 views19 pages

Python Function-1

The document provides an overview of Python functions, including their definition, calling, parameters, return statements, and built-in functions. It explains the concepts of pass by value and pass by reference, as well as how to use default parameter values and pass lists as arguments. Additionally, it outlines a project to create a Point of Sales (POS) system for a fast food chain, detailing required features and functionality.
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)
2 views19 pages

Python Function-1

The document provides an overview of Python functions, including their definition, calling, parameters, return statements, and built-in functions. It explains the concepts of pass by value and pass by reference, as well as how to use default parameter values and pass lists as arguments. Additionally, it outlines a project to create a Point of Sales (POS) system for a fast food chain, detailing required features and functionality.
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

PYTHON

FUNCTION
CPROG 2
OUTLINE
❑ FUNCTION
❑ DEFINE AND CALLA FUNCTION
❑ FUNCTION WITH PARAMETER
❑ RETURN STATEMENT
❑ PYTHON BUILT-IN FUNCTIONS
FUNCTION
➢a block of code which only
runs when it is called.
➢a way to group instructions.
➢it avoids repetition and makes
code reusable
➢functions created by you are
called user-defined functions.
DEFINE AND CALL A FUNCTION
➢ Use def to define a function.
➢Function has parameters (arguments) through
which we pass values. They are optional.
➢ A colon (:) to mark the end of function header.
➢One or more valid python statements make up the
function body with the same indentation level.
➢An optional return statement to return a value
from the function.
➢ To call the function, just use the function name.
Pass by Value vs Pass by Reference
Pass by Value
Any other operation performed will not have any effect
on the original value as the argument passed to the
function is copied. It only changes the value of the
copy created within the function.
def functionName(num):
num+=100
print("Inside function call ",num)

num=100
print("Before function call ",num)
functionName(num)
print("After function call ",num)
Pass by reference
This method passes a reference to the original
arguments, and any operation performed on the
parameter affects the actual value. It alters the value
in both function and globally.
def function(char):
[Link](‘B’)
print("Inside function call",char)

char=[‘A’]
print("Before function call",char)
function(char)
print("After function call",char)
FUNCTION WITH PARAMETER
➢Now let's define a function with parameters (also
called arguments ):

➢This time, when we call our function, we need to pass


it a value - we'll put the string "Batman" inside the
parentheses:
MULTIPLE
PARAMETERS/ARGUMENTS
➢By default, a function must be
called with the correct number
of arguments, that is, if your
function expects 2 arguments,
you have to call the function
with 2 arguments, not more,
and not less.
DEFAULT PARAMETER VALUE
➢If the function is without argument, it uses the
default value.
PASSING A LIST AS AN
ARGUMENT
➢A function can have any data type as an argument (string,
number, list, dictionary etc.), and it will be treated as the
same data type inside the function.
return statement
➢ We know that calling
the print() function displays
something to the screen.
➢ But all it does is display that
value - the value isn’t saved.
➢ Use return to give a value
to the caller.
return STATEMENT EXAMPLE
Python Built-in Functions
➢Built-in functions are defined as the functions
whose functionality is pre-defined in Python.
➢The first built-in function you used was print()
➢In python 3, there are 68 built-in functions.
Python Built-in Functions
Project Output
Point Of Sales
1. Create a POS of your own fast food chain.
2. You should have a log in screen, ask the user for username and password.
3. After logging in, display the username at the topmost right of the screen and the
MENU at the center. Menu should include. [ORDER], [SHOW TOTAL
SALES],[LOG OUT]
4. When a customer order, show the menu (with code) of what your selling with
corresponding prices, then calculate the total amount, compute change based on
the value of cash from the customer. Then add the sales per customer in a list or
dictionary.
5. When displaying the total sales, compute the sum of the sales’ list.
6. After logging out, display the log in screen.
7. Create a function for every menu.
Built-in Functions
Use 5 built-in functions of python.
Add a description of its usage.

You might also like