Python Functions

Python Function

  • In this topic, you'll learn about functions; what is a function, the syntax, components and types of a function. Also, you'll learn to create a function in Python.

What is a function in Python?

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

  • You can pass data, known as parameters, into a function.

  • A function can return data as a result.

  • Functions are a construct to structure programs. They are known in most programming languages, sometimes also called subroutines or procedures. Functions are used to utilize code in more than one place in a program. The only way without functions to reuse code consists in copying the code.

  • A function in Python is defined by a def statement.

Types Of Function:  

  1. Built in Functions  

  2. User Defined Functions

Syntax

                def function-name(Parameter list): 

                            statements, i.e. the function body

Creating a Function

  • In Python a function is defined using the def keyword.

Example

                    def my_function():
                         print("Hello from a function"

Calling a Function

  • To call a function, use the function name followed by parenthesis.

Example

                    def my_function():
                         print("Hello from a function")
                    my_function()                                  #Function Calling

 

 

 

Comments