Table of contents
- Python Functions
- Built-in Functions in Python
- User-defined Functions in Python
A python function is a block of python statements that are used to perform a certain task. As a programmer, you will encounter many situations when you have to perform a single action repeatedly. Instead of writing code multiple times, we can organize those statements in a function and give it a name.
Once we have done that, we can call the same function multiple times using a function name. Each time a function is called, all the statements in the function are executed. This way, we can write our code in a reusable and modular way.
Built-in Functions in Python
Built-in functions in python are those functions which are already defined in the python library. We have already used multiple python built-in functions in this tutorial such as print(), input(), range() and so on. Some of the built-in functions are specific to python objects like lists, dictionaries, string and so on. We will cover these and other built-in python functions later.
User-defined Functions in Python
Other than the functions that are already defined in the python library, you can also create functions to perform certain tasks that are needed repeatedly in your program. These types of functions that a programmer or user defines are called user-defined functions.
How to Define a Function in Python?
The “def” keyword is used to define a python user-defined function followed by a function name, parentheses and a colon. The parenthesis may contain input parameters or arguments. After the definition, the statements inside the function body should be indented equally.
You can write an optional comment statement inside a function body called documentation string or docstring. The docstring should be enclosed by quotation signs. A function may also have a return statement at the end of the function body. If there is no return statement, the function will return None value.
Python function syntax:
def function_name (parameters): "docstring" function body return statement
def printhello (): "A function to print hello" print("Hello!")
In the above example, the function name is “printhello” and its task is to print “Hello!” on the console when it is called. This is a simple example. We will look at more complex examples with return statements and parameters later in this post as well as the next post.
Python Function Naming Rules and Conventions
Similar to variable naming rules, there are some rules and conventions that you need to follow while naming python functions as:
- A function name can start with either of lowercase (a-z), uppercase (A-Z) alphabet, or an underscore (_).
- Other characters of a function name can be lowercase (a-z), uppercase (A-Z) alphabets, digits(0-9), or underscores(_).
- You can not use a keyword or reserved word as a function name.
- It is recommended to use a descriptive word as a function name so that it can be understood what a function will do by looking at the name itself.
How to Call a Function in Python?
A python function can be called using the function name given to it while defining a function followed by a parenthesis. If a function requires arguments, those need to be passed inside the parenthesis. The statements inside a function can only be executed when it is called.
Python function call syntax:
Here, we call the function “printhello()” that we defined above. Since it doesn’t have any parameters, we do not pass any arguments. We will learn about the parameters and arguments in the next post.
Python return Statement
As mentioned earlier, a python function may contain an optional return statement. A “return” keyword is used followed by a value or variable or an expression that is to be returned when a function is called. If there is no return statement in a function, value None is returned.
def returneg(): a = "This is a return example." return a print(returneg())
This is a return example.
In the example above, the function “returneg()” returns a variable “a” which stores a string “This is a return example.”. So, when the function is called inside a “print()” function, the string is printed on the console.