How to Define Functions in Python

In this tutorial we want to learn How to Define Functions in Python, so if you are a programmer and you do programming in different programming languages, you will know that functions are one of the fundamental building blocks of programming, we can say that function is a block of reusable code that performs specific task, in Python functions are defined using def keyword. in this article we want to talk about the syntax and features of Python function definition.

 

 

 

 

How to Define Functions in Python?

First of all let’s talk about basic syntax for defining a function,  in Python you can define a function like below, now let’s me describe this code, def keyword is followed by the name of the function and pair of parentheses. any parameters that the function takes are listed inside parentheses. body of the function is indented and contains statements that the function executes.

 

 

 

 

This is an example of a simple function that takes no parameters and returns a string like this.

 

 

So right now we have our function, but we can not see the result, because when you define a function, you need to call that function in your code like this.

 

 

 

Run the code and you will see this result

How to Define Functions in Python
How to Define Functions in Python

 

 

 

Function Parameters in Python

Now let’s talk about function parameters, functions in Python can take one or more parameters. Parameters are listed inside parentheses when the function is defined. when function is called, values can be passed in for parameters. the values that are passed are called as arguments.

 

 

This is the example of two parameters that returns the sum of x and y.

 

 

 

Run the code and this will be the result

Parameters in Functions
Parameters in Functions

 

 

 

Default Parameters in Python Functions

There is another thing that you can use in Python Function and that is called default parameter, sometimes we want a parameter to have a default value if no argument is passed. you can do that by specifying a default value for the parameter in the function definition.

 

 

This is an example of a function that takes one required argument and one optional argument:

In this example greeting parameter has default value of Hello. if we call greet(“codeloop.org”), function will return Hello, codeloop.org.

 

 

 

This will be the result

Default Parameters - How to Define Functions in Python
Default Parameters – How to Define Functions in Python

 

 

 

Return Value in Python Functions

Functions in Python can return a value using return keyword. when return keyword is encountered, function stops executing and returns the value specified.

 

 

 

Docstrings in Python

Docstrings are used to provide documentation for functions. they are enclosed in triple quotes and placed immediately after the function header. Docstrings should describe what function does, what parameters it takes, and what it returns.

 

 

 

 

For accessing the docstring of a function, we can use __doc__ attribute like this:

 

Subscribe and Get Free Video Courses & Articles in your Email

 

Leave a Comment

Share via
Copy link
Powered by Social Snap
×