Python function definition

In this Python article we want to learn about Python function definition, 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 explore the syntax and features of Python function definition.

 

 

Python function definition

First of all let’s talk about basic syntax for defining a function,  in Python you can define a function like this, 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.

 

 

This will be the result

Python function definition
Python function definition

 

 

 

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. values passed in are referred to 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

Function definition in Python
Function definition in Python

 

 

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 in. this can be accomplished 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(“geekscoders.com”), function will return Hello, geekscoders.com.

 

 

This will be the result

Python Function Default Parameter
Python Function Default Parameter

 

 

if we call greet(“geekscoders”, “Good morning”), function will return Good morning geekscoders like this.

Python Function Default Parameter
Python Function Default Parameter

 

 

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

 

 

 

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:

 

 

Learn More on Python Dictionaries 

Leave a Comment