• Study Materials
    • InfyTQ Archive
    • Infosys Archive
    • TCS Archive
    • Accenture Archive
    • AMCAT Archive
    • Capgemini Archive
    • Cisco Archive
    • CoCubes Archive
    • Cognizant(CTS) Archive
    • Deloitte Archive
    • DXC Archive
    • Goldman Sachs Archive
    • Hexaware Technologies Archive
    • LTI Archive
    • MindTree Archive
    • Virtusa Archive
    • Wipro Archive
  • Interview Preparation
    • C Interview Questions
    • Data Structures Interview Questions
    • DBMS Interview Questions
    • HR Interview Questions
    • Java Interview Questions
    • Operating System Interview Questions
    • Python Interview Questions
    • SQL Query Interview Questions
  • Programming
    • C Programming MCQs
    • C Code Snippets – Output Questions
    • Python Code Snippets – Output Questions
    • Java Code Snippets – Output Questions
  • Aptitude
    • Verbal Ability for Placements
    • Quant for Placements
  • Mock Interview Series
  • Register
  • Login
CODE OF GEEKS

We at CODE OF GEEKS, aim at providing best and quality content for our users at no extra cost.

    • Study Materials
      • InfyTQ Archive
      • Infosys Archive
      • TCS Archive
      • Accenture Archive
      • AMCAT Archive
      • Capgemini Archive
      • Cisco Archive
      • CoCubes Archive
      • Cognizant(CTS) Archive
      • Deloitte Archive
      • DXC Archive
      • Goldman Sachs Archive
      • Hexaware Technologies Archive
      • LTI Archive
      • MindTree Archive
      • Virtusa Archive
      • Wipro Archive
    • Interview Preparation
      • C Interview Questions
      • Data Structures Interview Questions
      • DBMS Interview Questions
      • HR Interview Questions
      • Java Interview Questions
      • Operating System Interview Questions
      • Python Interview Questions
      • SQL Query Interview Questions
    • Programming
      • C Programming MCQs
      • C Code Snippets – Output Questions
      • Python Code Snippets – Output Questions
      • Java Code Snippets – Output Questions
    • Aptitude
      • Verbal Ability for Placements
      • Quant for Placements
    • Mock Interview Series
    • Register
    • Login
CODE OF GEEKS
CODE OF GEEKS
  • Study Materials
    • InfyTQ Archive
    • Infosys Archive
    • TCS Archive
    • Accenture Archive
    • AMCAT Archive
    • Capgemini Archive
    • Cisco Archive
    • CoCubes Archive
    • Cognizant(CTS) Archive
    • Deloitte Archive
    • DXC Archive
    • Goldman Sachs Archive
    • Hexaware Technologies Archive
    • LTI Archive
    • MindTree Archive
    • Virtusa Archive
    • Wipro Archive
  • Interview Preparation
    • C Interview Questions
    • Data Structures Interview Questions
    • DBMS Interview Questions
    • HR Interview Questions
    • Java Interview Questions
    • Operating System Interview Questions
    • Python Interview Questions
    • SQL Query Interview Questions
  • Programming
    • C Programming MCQs
    • C Code Snippets – Output Questions
    • Python Code Snippets – Output Questions
    • Java Code Snippets – Output Questions
  • Aptitude
    • Verbal Ability for Placements
    • Quant for Placements
  • Mock Interview Series
  • Register
  • Login

Cart

Introduction to Functions in Python with Example

  • November 28, 2021
  • CODE OF GEEKS
  • 0

Function in Python refers to the block of statements that are intended to perform a specific task. To be specific, there are many Python functions that are used to perform a particular task.

For instance, print() is a function used to display the information on the output screen. Similarly, we have input(), for taking the user input. Such functions are known as built-in functions. In the same way, Python also provides the flexibility to created our own functions. These are known as user-defined functions.



Why we use functions in Python ?

1. Function allows us to follow OOPs principle like Modularity. Module represents a part of the program. Usually, it is suggested to divide the problem statement into various subtasks. Modular Programming makes thing easy.

2. Function refers to the block of code to perform specific tasks.

3. Once written, functions can be reused whenever required.

4. Code Maintenance and debugging is easy while working with functions.

5. Adding functions to the code will reduce the length of the program.

Defining a function

Python allows us to define the function using def keyword. Let us see how function definition works :

def function_name(para1, para2, ...):
    function statements

As mentioned, we use def keyword to define a function along with function name and parameters(if any).

Consider following definition :

def multiply(num1, num2):
    return num1 * num2

def represents the initial point of function definition. Here, ‘multiply’ is the function name. After that, we need to add parenthesis ( ) mandatorily as interpreter will get know that this is function, not a variable or any other thing.

num1, num2 represents the variable, they are being used as function parameters. A function may or may not have function parameters.

After ( ), we add colon (:) that represents the function body. This function body is coined as suite which denotes the block of statements.

Do you know ?

It is recommended to write string as the first statement in the function body. This string is referred to as docstring. It can have one or more lines. Below code specify the use of docstring :

def multiply(a, b) :
”’This function returns the product of a, b. Example of docstring”’
return None



After this, time is to write some cool code as a part of function body that defines the logic for a task. Make sure, your code should follow the correct indentation.

Indentation refers to the spaces at the beginning of a code line. Python uses indentation to indicate a block of code.

Calling a function

When we define a function, it does not have the ability to run on its own. It will only run once it is called. So, the next step is to call the function using its name. A defined function is called with its name and parameters.

Let’s call the function that we defined above :

multiply(10, 20)

Here, we are calling the multiply() function that we defined above to calculate the product of two numbers. In the above code, 10, 20 are arguments.

Now, let’s try to connect our complete code :

def multiply(num1, num2):
    return num1 * num2
print(multiply(10, 20))

Output : 200



In the above code, we are calling multiply() function with two arguments 10 & 20. This function then returns the product of two numbers.

Storing values returned by a function to a variable

def multiply(num1, num2):
    return num1 * num2
result = multiply(10, 20)
print(result)

Here, in the above code, we are storing the value returned by the function to variable result.

Points to Remember – Python Functions

1. In Python, it is possible to assign a function to a variable.

2. In Python, it is possible to have nested functions (function within function).

3. In Python, it is possible to pass a function as parameter to another function.

4. In Python, it is possible to return a function from one function.

That’s all in this lesson.





Tags: function in pythonfunction pythonpython functions. functions in python
  • Previous Formal and Actual Parameters in Python Function
  • Next return Statement in Python with Example

Leave a Reply Cancel reply

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

Table of Content – Python

  1. Introduction to Python Programming
    • Python and its features
    • Key Differences : C vs Python
    • Key Differences : Java vs Python
    • Python Flavors
    • PVM and Memory Management in Python

  2. Python Programming Fundamentals
    • 'Hello World' using Python
    • Comments in Python
    • Variables and Garbage Collection in Python
    • Datatypes in Python
    • Determining the Datatype of Literal | type() method
    • Tokens in Python
    • Keywords in Python
    • Identifiers and Naming Convention in Python
    • Literals in Python

  3. Python Operators
    • Operators in Python
    • Python Mathematical Functions

  4. Python User Inputs
    • Taking Inputs | input() function in Python

  5. Python Control Statements
    • Control Statements in Python
    • if-elif-else Statements in Python
    • Looping in Python
    • while loop in Python
    • while-else loop in Python
    • for loop in Python
    • for-else loop in Python
    • Infinite loops in Python
    • Nested loops in Python
    • break Statement in Python
    • continue Statement in Python
    • pass Statement in Python
    • assert Statement in Python
    • return Statement in Python

  6. Python Functions
    • Introduction to Python Functions
    • Python Functions vs Python Methods
    • Local and Global Variables in Python
    • Formal and Actual Arguments in Python
    • Recursion in Python Functions
    • Lambda Functions in Python
    • Function Decorators in Python
    • Function Generators in Python
CODE OF GEEKS

Subscribe to Newsletter

CODE OF GEEKS

Learn | Code | Achieve

Reach us

[email protected]
We at CODE OF GEEKS, aim at providing quality content to our users at no cost.
CODE OF GEEKS

Important Pages

About us
Advertise
Privacy Policy
Terms and Conditions

Placements – Study Materials

TCS NQT     Wipro     CapGemini
Accenture     MindTree     CTS
DXC     Hexaware Technologies     AMCAT
CoCubes     Goldman Sachs     Dell
Cisco     Deloitte     Virtusa     LTI     Infosys   

Courses

  • Mail Us
  • Login/Register

Recent Posts

Strings in Python and Operations on Python Strings
  • January 18, 2022
Hedger
  • January 15, 2022

Copyright 2022 CODE OF GEEKS. All Rights Reserved.

  • →