Code in Python
Machine Learning and Deep Learning Blogs

All you need to know about Functions in Python

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.


def functionname( parameters ):
  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.

  1.  The function begins with the keyword def followed by the function name and parentheses ( ).
  2. You should put all your input parameters or arguments within these parentheses. You can define parameters inside these parentheses.
  3. 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.
  4.  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.

For example

# Defining function here
def displaynum ( num ):
  # notice the keyword def, function name displaynum and a number parameter num
  #function body below
  print("Number passed in the function is ", num)

The above block of code defines the function displaynum. Now in order to execute the function, we will call it.

# Calling the function
n = 12
b = 12.5
displaynum ( n )
displaynum ( b )

After code execution, the result is –

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.

For Example:

def student ( name, marks = 90 ):
  print ( "Name: ", name )
  print( "Marks: ",marks )
# function call
nm = "Charles"
student(nm, marks = 95)
student (nm) #default value taken

After code execution, result is –

Name: Charles
Marks: 95
Name: Charles
Marks: 90

Akarsh Singh
Follow me

Akarsh Singh

Founder, Code in Python. Web and Android App Developer. He loves Python and wants to share his knowledge via this website. Follow him on Facebook, Twitter and Github.
Akarsh Singh
Follow me

Latest posts by Akarsh Singh (see all)


  1. 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.

    Any suggestions or advice would be greatly appreciated.
    Many thanks

  2. It is actually a great and useful piece of info. I am happy that you shared this helpful info with us. Please keep us up to date like this. Thanks for sharing.

  3. 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.

  4. 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

  5. I can’t believe how great this site is. You keep up the good work. That’s my advice pal.

  6. Just fabulous. Who wrote this and how can we get more?

  7. Your means of explaining all in this paragraph is
    really nice, every one be able to easily be aware of it, Thanks
    a lot.

  8. 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.

  9. Currently it appears like WordPress is the top blogging
    platform available right now. (from what I’ve read) Is that what you
    are using on your blog?

  10. Hi there! I simply want to offer you a big thumbs up for
    your excellent info you have got right here
    on this post. I am coming back to your web site for more soon.

  11. This is a topic that is close to my heart… Cheers! Where
    are your contact details though?

  12. It’s actually a nice and helpful piece of information. I’m glad that you simply shared this helpful info with us.
    Please stay us informed like this. Thanks for sharing.

  13. 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!

Leave a Reply

Your email address will not be published.


© 2019 Code in Python