Python Function

Python Function

Functions are the key part of most of the  programming languages. Function  is a  group of statements, used to perform a specific task.

In this article we will learn:

  1. What is function?
  2. Advantages of functions
  3. Python function (definition , calling)
  4. Documentation string in function
  5. Return vs print
  6. Function parameters
  7. Parameters vs Arguments
  8. Parameter with default values
  9. Anonymous function (lambda)
  10. Filter function

What is Function?

Functions are the key part of most of the  programming languages. Function is a group of statements, used to perform a specific task. It is used to utilize code at more than one place.  We can divide huge problem into small parts called functions or modules. Suppose in program, there are lines of code which need to repeat more than one time, in that case instead of copy these lines again and again, we can create a function for that and can use it whenever needed (by calling function). In other words we can say function is a block of reusable code, used to perform a specific task.

 

Advantages of Functions:

  1. Clean code: Functions improves the clarity of the code we can say results in well structured code.
  2. Decomposition :  It breaks the complex problem into smaller parts (functions, also called modules), which is easy to  understand and read.
  3. Speed and Time:  As we need not to write code again and again so it improves the speed and saves time.
  4. Re usability: We can use a function more than one time in more than one programs by calling it.
  5. Data hiding:  By  using function we can hide extra information.
  6. Reduce Duplicate Code

Python Function:

In python, a function is defined with def keyword. A normal function has three main parts these are:

  1. Function definition
  2. Body of function
  3. Function calling

In python function is defined using def  keyword, followed by the name of the function , parenthesis and colon(:). The body of the function starts with the indentation (space). Unlike other languages, python does not use curly braces , semicolons etc. Function blocks are separated  by indentation.

A function is a block of code which only runs when it is called.

Syntax:

function
python function

Types of functions in python:

There are two types of functions in python –

  • Built-in
  • User-defined

Built-in functions are the part of the python language example :- len() , print() etc.

User-defined functions are created by the user itself by using def keyword.

 Doc-string in python functions:

Python documentation strings (or docstrings) provide a convenient way of associating documentation with Python modules, functions, classes, and methods. Doc-string is used like a comment to document a specific segment of code.

The docstrings are declared using  “””triple double quotes”””  just below the class, method or function declaration

The docstrings can be accessed using the __doc__ method of the function.

Function Example:

Here is an python function with a documentation string:

function with docstring
function with docstring

Output:

 

function with docstring
function with docstring

Return() vs Print()

Every function returns a value or result. If return is not specified in function than it will return none. It is the last statement of any function, which means end of block. If there is not return statement then function will execute till end of code(i.e till last statement). This value can further use in other operations, or functions. This return value may not visible to the user, but computer can use it in further operations.

Print()  just shows the human user a string representing what is going on inside the computer. The computer cannot make use of that printing. It will not in any way affect a function. Print() is simply there for the human user’s benefit, useful for understanding how a program works.

Example:

 

print vs return
print vs return

Output:

Output: return vs print
Output: return vs print

Function Parameters:

We can pass additional information to a function by using parameters. Parameters are specified after the function name, inside the parentheses.We can pass number of parameters in function, separating by comma. We can also pass parameters with a default value in a function.

Example:

function with parameters
function with parameters

Output:

output: function
output: function

Parameters vs Arguments:

Most people thinks there is no difference between parameters and arguments, but there is. A parameter is a variable in a function definition. Arguments are the data members that we pass in function parameters at the time of function calling.. In other words we can say, parameter is a variable in the declaration of function. Argument is the actual value of this variable that gets passed to function.

parametes vs arguments

Function with default parameters:

We can pass default parameter values with a function.  During calling a function if argument values  passed than function will use these values otherwise it will use default values.

Example:

default parameter values
default parameter values

Output:

output: default parameters
output: default parameters

Anonymous function:

In python, anonymous function is a function, that defined without a name. Normally in python we use def keyword to declare a function. Where anonymous function is defined with the lambda keyword, that is why it is also called lambda functions.Lambda expression has no name that is why it is called anonymous function.

Lambda forms can take any number of arguments but return just one value in the form of an expression. They cannot contain commands or multiple expressions. It basically breakdowns the function.

An anonymous function cannot be a direct call to print because lambda requires an expression

Syntax:

lambda arguments: expression

Example:

Anonymous function
Anonymous function

Output:

output: Anonymous function
output: Anonymous function

Filter function:

In python filter function takes two arguments,  function and sequence. It returns those items of the sequence for which the function is true.

Syntax:

filter(function, sequence)

Example:

In the example below, we are using lambda expression (function) as first parameter of filter function, you can take any normal function also.

filter function
filter function

In above example filter() has two parameters –

1st Parameter – seq (List of numbers)

2nd Parameter – lambda function

In line 4 , we converts the  output into list which is list of even numbers as shown below:

Output:

Output: filter function
Output: filter function

So it is all about the python function, Thanks

 

 

 

 

61 thoughts on “Python Function

Leave a Reply

Your email address will not be published. Required fields are marked *

  1. You actually make it seem so easy with your presentation but I find this matter to be really something which I think I would never understand. It seems too complicated and extremely broad for me. I am looking forward for your next post, I will try to get the hang of it!

  2. We’re a gaggle of volunteers and opening a new scheme in our community. Your web site offered us with useful info to work on. You’ve done an impressive process and our entire community will probably be grateful to you.

  3. Thank you so much for giving everyone an extremely splendid opportunity to read critical reviews from this web site. It is often so enjoyable and stuffed with a lot of fun for me and my office peers to search the blog at the very least three times per week to learn the fresh guides you have. And definitely, I am at all times impressed for the exceptional ideas you give. Some 4 points in this posting are particularly the most beneficial we’ve ever had.

  4. Nice post. I was checking constantly this blog and I’m impressed! Very useful info specifically the last part 🙂 I care for such information a lot. I was seeking this certain info for a very long time. Thank you and good luck.

  5. I simply couldn’t leave your website before suggesting that I really loved the standard info an individual provide for your guests? Is gonna be again frequently in order to check up on new posts

  6. I am not sure where you are getting your info, but good topic. I needs to spend some time learning more or understanding more. Thanks for magnificent info I was looking for this information for my mission.

  7. You really make it seem so easy with your presentation but I find this matter to be actually something which I think I would never understand. It seems too complicated and very broad for me. I am looking forward for your next post, I’ll try to get the hang of it!

  8. You can definitely see your enthusiasm in the work you write. The arena hopes for more passionate writers such as you who are not afraid to mention how they believe. Always follow your heart.

  9. I’ve been surfing online more than 3 hours today, yet I never discovered any fascinating article like yours. It is lovely price enough for me. In my view, if all site owners and bloggers made just right content material as you did, the internet will probably be a lot more useful than ever before.

  10. I just want to tell you that I’m all new to blogs and absolutely loved your website. Most likely I’m planning to bookmark your website . You actually come with beneficial writings. Regards for sharing your web page.

  11. This design is wicked! You definitely know how to keep a reader amused. Between your wit and your videos, I was almost moved to start my own blog (well, almost…HaHa!) Great job. I really loved what you had to say, and more than that, how you presented it. Too cool!

  12. Do you have a spam problem on this site; I also am a blogger, and I was wanting to know your situation; many of us have created some nice practices and we are looking to trade methods with other folks, why not shoot me an email if interested.

  13. There are definitely quite a lot of details like that to take into consideration. That could be a nice point to convey up. I provide the ideas above as general inspiration however clearly there are questions just like the one you convey up where the most important thing can be working in trustworthy good faith. I don?t know if finest practices have emerged around things like that, however I’m certain that your job is clearly recognized as a good game. Both girls and boys feel the influence of only a moment’s pleasure, for the rest of their lives.

  14. Hola! I’ve been following your website for a long time now and finally got the bravery to go ahead and give you a shout out from Atascocita Texas! Just wanted to mention keep up the excellent work!

  15. I think this is among the most vital info for me. And i am glad reading your article. But want to remark on some general things, The website style is wonderful, the articles is really nice : D. Good job, cheers

  16. Hey there very cool website! Guy. Great aplikasi berita saham terbaru. Wonderful. I will bookmark your website in addition to go ahead and take bottles likewise? Now i’m grateful to find plenty of beneficial data in this article in the distribute, we would like acquire additional methods about this reverence, many thanks for discussing.

  17. Excellent pieces. Keep writing such kind of info on your site.
    Im really impressed by your site.
    Hello there, You’ve done an excellent job.
    I will definitely digg it and personally suggest to my friends.
    I am sure they will be benefited from this web
    site.

  18. The next time I read a blog, I hope that it doesnt disappoint me as much as this one. I mean, I know it was my choice to read, but I actually thought youd have something interesting to say. All I hear is a bunch of whining about something that you could fix if you werent too busy looking for attention.

  19. Fantastic challenges totally, you merely acquired a whole new readers. What could you recommend in relation to your current submit for . youtube terbaikyou to developed a 1 week previously? Virtually any optimistic?

  20. Admiring the time and energy you put into your site and in depth information you present. It’s awesome to come across a blog every once in a while that isn’t the same out of date rehashed information. Wonderful read! I’ve saved your site and I’m including your RSS feeds to my Google account.

Please wait...

Subscribe to our newsletter

Want to be notified when our article is published? Enter your email address and name below to be the first to know.