You may be faintly familiar with functions by constantly using the built-in functions that Python provides. For example print(), input(), etc. are some of the built-in functions in Python.

A function consists a block of code with which we want to perform some kind action. Now instead of writing that particular code repeatedly when a certain action is required, we can just call the function. This promotes code reusability and modularity.

Functions help us to write clean codes. The code we write becomes more readable. Apart from built-in functions provided by Python, we can create our own functions these functions are called user-defined functions.

Syntax:

def functionname( parameters ):
function_body
return (expression)

Defining Functions

You have to define the functions and provide them the required functionality. You define a function by using the syntax given and writing the logic in the function body. Here are some rules to define a function in Python.

The function begins with the keyword def followed by the function name and parentheses ( ).

You should put all your input parameters or arguments within these parentheses. You can define parameters inside these parentheses.

The function starts with a colon (:). The code block within every function is the function body. The function body is indented to show that the function is defined.

The statement return (expression) exits a function. The keyword return also helps in passing back an expression to the caller of the function. A return statement with no arguments or expression to pass is the same as return None.

Calling Functions

When we define a function we only give it a name, specify the parameters that we want to be included in the function and write the blocks of code which we call functions body. Once the basic definition of a function is completed, you can execute the function by calling it from some other function or directly from the Python prompt.

Number passed in the function is 12
Number passed in the function is 12.5

Default Arguments

Many times we come across a situation where we want a default value to be taken if the user has not input any value from their side. A default argument comes to rescue in these situations if we are defining a function. A default argument is an argument that assumes a default value if a value is not provided in the function call for that argument.

14 Comments

I know this if off topic but I’m looking into starting my own blog and was curious what
all is required to get setup? I’m assuming having a blog like yours
would cost a pretty penny? I’m not very internet smart so I’m not 100% positive.

Thank you for every other excellent post. The place else could anyone get that kind of information in such an ideal approach of writing? I’ve a presentation subsequent week, and I am on the look for such info.

Wonderful beat ! I wish to apprentice at the same time as you amend your web site, how can i subscribe for a weblog site? The account helped me a applicable deal. I have been a little bit familiar of this your broadcast offered brilliant transparent concept

Please let me know if you’re looking for a article author
for your site. You have some really good posts and
I think I would be a good asset. If you ever want to take some of the load off,
I’d absolutely love to write some articles for your blog in exchange for
a link back to mine. Please shoot me an email if interested.
Regards!

Howdy! I could have sworn I’ve been to this website before but after checking through
some of the post I realized it’s new to me. Anyhow,
I’m definitely glad I found it and I’ll be book-marking and
checking back frequently!