PYTHON FUNCTIONS (1)
PYTHON FUNCTIONS (1)
What is a function
A function is a named code block that performs a job or returns a value.
To do so, you wrap the code in a function and use this function to perform
the task whenever you need it.
For example, whenever you want to display a value on the screen, you need
to call the print() function. Behind the scene, Python runs the code
inside the print() function to display a value on the screen.
In practice, you use functions to divide a large program into smaller and
more manageable parts. The functions will make your program easier to
develop, read, test, and maintain.
def greet():
""" Display a greeting to users """
print('Hi')
Code language: Python (python)
1) Function definition
A function definition starts with the def keyword and the name of the
function (greet).
If the function needs some information to do its job, you need to specify it
inside the parentheses (). The greet function in this example doesn’t
need any information, so its parentheses are empty.
2) Function body
All the indented lines that follow the function definition make up the
function’s body.
The line print('Hi') is the only line of actual code in the function body.
The greet() function does one task: print('Hi').
Calling a function
When you want to use a function, you need to call it. A function call
instructs Python to execute the code inside the function.
greet()
Code language: Python (python)
Hi
Code language: Python (python)
def greet(name):
Code language: Python (python)
When you add a parameter to the function definition, you can use it as a
variable inside the function body:
def greet(name):
print(f"Hi {name}")
Code language: Python (python)
And you can access the name parameter only within the body of the
greet() function, not the outside.
When you call a function with a parameter, you need to pass the
information. For example:
greet('John')
Code language: Python (python)
Output:
Hi John
Code language: Python (python)
The value that you pass into a function is called an argument. In this
example 'John' is an argument.
Also, you can call the function by passing a variable into it:
first_name = 'Jane'
greet(first_name)
Code language: Python (python)
An argument is a piece of data that you pass into the function. For
example, the text string 'John' or the variable jane is the function
argument.
Returning a value
A function can perform a task like the greet() function. Or it can return a
value. The value that a function returns is called a return value.
To return a value from a function, you use the return statement inside the
function body.
return value
Code language: Python (python)
def greet(name):
return f"Hi {name}"
Code language: Python (python)
When you call the greet() function, you can assign its return value to a
variable:
greeting = greet('John')
Code language: Python (python)
print(greeting)
Code language: Python (python)
The new greet() function is better than the old one because it doesn’t
depend on the print() function.
Later, you can reuse the greet() function in other applications. For
example, you can use it in a web application to greet users after they log in.
total = sum(10,20)
print(total)
Code language: Python (python)
Output:
30
Code language: Python (python)
In this example, the sum() function has two parameters a and b, and
returns the sum of them.
When you call the function, you need to pass all the arguments. If you pass
more or fewer arguments to the function, you’ll get an error.
Summary
● A Python function is a reusable named block of code that performs a
task or returns a value.
● Use the def keyword to define a new function. A function consists of
function definition and body.
● A function can have zero or more parameters. If a function has one or
more parameters, you need to pass the same number of arguments
into it.
● A function can perform a job or return a value. Use the return
statement to return a value from a function.