Function in Python | with an Example

 Function in Python with an example | Everything you need to know about functions in python.


Function in Python

  • A function is a group of logically related statements
  • Grouped into one block of statement
  • Re-usable

As we already know, Python gives you many built-in functions like print (), etc. but we can also create our own functions. These functions are called user-defined functions.


Defining a Function

You can define functions to provide the required functionality. Here are simple rules to define a function in Python.

·        Function blocks begin with the keyword def followed by the function name and parentheses (( )).

·        Any input parameters or arguments should be placed within these parentheses. You can also define parameters inside these parentheses.

·        The code block within every function starts with a colon (:) and is indented.

·        The statement return [expression] exits a function, optionally passing back an expression to the caller. A return statement with no arguments is the same as a return of none.



Calling a Function:-



Method using the parameter:


Naming convention for method:

   ·         Method name should be in small case

   ·     If the method name having two words separate the method name with an underscore (_) to improve readability.

   ·         All the identifiers rules are applicable.


#1 Case – Keyword Arguments



#2 Case – Positional Arguments


#3 Case – Keyword with Positional Arguments


#4 Case – Variable Arguments

*args are mostly used in function definitions. *args allow you to pass a variable number of arguments to a function. What variable means here is that you do not know beforehand how many arguments can be passed to your function by the user so in this case, you use this keyword. args is used to send a non-key worded variable-length argument list to the function. Here’s an example to help you get a clear idea:



Return Type of Function:

Case-1:

  •  The default data type of function is none.
  • · If the user is not returning any value then python will return none statement
  • · It is always best practice to write a return statement at the end of the function otherwise the statements which are present after the return statement will not be executed.
  • · User also can return his own value


Case-2



Question: Can we have multiple return statements?

Case-3


Case-4

Multiple values we can return into a single line:-



Case-5

Boolean value also we can return:-


Default value store in the form of a tuple:


By default, if only one return value python will return the default value as in the form of a tuple.

In function, we can return list and string as well.


Recursion in Python:-

We know that in Python, a function can call other functions. It is even possible for the function to call itself. These types of the construct are termed recursive functions.

 

1.  Only the outer function will execute because the inner function is not called


2. We can have the same function name as the outer and inner




3.   Call both inner and outer function



4. When an inner function is not called



5.  Recursive Call :


The above code executes for an infinite loop because the outer function is called repeatedly on its own.

Question:-Write a python program to find factorial of a number



Note:- One function can have multiple return statements but it should be aligned with the condition.











Comments

Popular Post

Root Cause Analysis in Testing | Easy way to explain in interview about RCA

Agile Testing Interview Questions and Answers | Easy way to explain the Agile Testing Interview Questions.

Data Types in Python with explanation

Static and Non Static Method in Python | Class Method in Python

How to Run Python Code in IDLE

OOPS in python | Object Oriented Programming Languages and Systems | Static and Non Static in Python

Python IDEs and Code Editor | PyCharm

What is the difference between client-side validation and server-side validation?

Operators in Python and Types of Operators in Python

How to Download Python ?