Code in Python
Machine Learning and Deep Learning Blogs
search

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.

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.

  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)
  return

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 )
  return
# 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)

5 Comments

  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

Leave a Reply

Your email address will not be published.

*

© 2018 Code in Python