DEV Community

Cover image for Introduction To Python Functions
Pope☦
Pope☦

Posted on

Introduction To Python Functions

What is a function in Python?

In Python, a function is a group of related statements that performs a specific task.

Functions help break our program into smaller and modular chunks. As our program grows larger and larger, functions make it more organized and manageable.

Furthermore, it avoids repetition and makes the code reusable.

User-Defined functions in python

Python lets us group a sequence of statements into a single entity, called a function. A Python function may or may not have a name.

Advantages of user-defined function sin python

  1. This Python Function help divide a program into modules. This makes the code easier to manage, debug, and scale.
  2. It implements code reuse. Every time you need to execute a sequence of statements, all you need to do is to call the function.
  3. This Python Function allow us to change functionality easily, and different programmers can work on different functions.

How to define a function.

To define your own Python function, you use the ‘def’ keyword before its name. And its name is to be followed by parentheses, before a colon(:)

Rules for naming python function (identifier)

We follow the same rules when naming a function as we do when naming a variable.

It can begin with either of the following: A-Z, a-z, and underscore().
The rest of it can contain either of the following: A-Z, a-z, digits(0-9), and underscore(
).
A reserved keyword may not be chosen as an identifier.
It is good practice to name a Python function according to what it does.

Python function parameters

Sometimes, you may want a function to operate on some variables, and produce a result. Such a function may take any number of parameters. Let’s take a function to add two numbers.

Python return statement

A Python function may optionally return a value. This value can be a result that it produced on its execution. Or it can be something you specify- an expression or a value.

Calling a Python function

To call a Python function at a place in your code, you simply need to name it, and pass arguments, if any. Let’s call the function hello() that we defined in section b.

hello()


Scope and Lifetime of Variables in Python

A variable isn’t visible everywhere and alive every time. We study this in functions because the scope and lifetime for a variable depend on whether it is inside a function.

Scope

A variable’s scope tells us where in the program it is visible. A variable may have local or global scope.

Local Scope- A variable that’s declared inside a function has a local scope. In other words, it is local to that function.

Global scope- When you declare a variable outside python function, or anything else, it has global scope. It means that it is visible everywhere within the program.

Deleting Python function

We have seen how to delete a variable. Similarly, you can delete a function with the ‘del’ keyword.

Python Built-in Functions

In various previous lessons, we have seen a range of built-in functions by Python. This Python function apply on constructs like int, float, bin, hex, string, list, tuple, set, dictionary, and so. Refer to those lessons to revise them all.

Python Lambda Expressions

As we said earlier, a function doesn’t need to have a name. A lambda expression in Python allows us to create anonymous python function, and we use the ‘lambda’ keyword for it. The following is the syntax for a lambda expression.

lambda arguments:expression

It’s worth noting that it can have any number of arguments, but only one expression. It evaluates the value of that expression, and returns the result.

Python Recursion Function

A very interesting concept in any field, recursion is using something to define itself. In other words, it is something calling itself. In Python function, recursion is when a function calls itself. To see how this could be useful, let’s try calculating the factorial of a number. Mathematically, a number’s factorial is:

n!=n*n-1*n-2*…*2*1

Conclusion

It is important to revise in order to retain information. In this lesson, we learned about the Python function. First, we saw the advantages of a user-defined function in Python. Now we can create, update, and delete a function. And we know that a function may take arguments and may return a value. We also looked at the scope and lifetime of a variable.

Top comments (0)