Sunday, March 19, 2023

Python Functions overview

Python Functions overview

Function:

A named blocks of code that are designed to do one specific job is called as Function.

Block in python:

A block is one or more lines of code, grouped together so that they are treated as one big sequence of statements while execution.

Main advantages of function:

It avoids repetition and makes a high degree of code reusing.

It provides better modularity for your application.

Recursive Function:

A Function which calls itself is called as Recursive Function.

Different types of function:

      1.User-defined functions

      2.Built in functions

      3.Lambda functions

      4.Recursive functions

1.   User defined function:

      Functions defined by the users themselves are called User defined functions.

      Syntax :

           def<function_name ([parameter 1, parameter 2.....]) > :
      < Block of statement >
      return < expression / None>

      Example:

           def welcome( ):

                       print(“Welcome to Python”)

                       return

2.   Built-in functions:

      Functions which are using Python libraries are called Built-in function.

      Example:

           print ()

3.   Lambda function:

      Lambda function is mostly used for creating small and one-time anonymous function.

      Lambda functions are mainly used in combination with the functions like filter(), map() and reduce().

      Syntax of Lambda function (Anonymous Functions):

      lambda [argument(s)]: expression

4.   Recursive function:

A recursive function calls itself. Imagine a process would iterate indefinitely if not stopped by some condition! Such a process is known as infinite iteration.

The condition that is applied in any recursive function is known as a base condition. A base condition is must in every recursive function otherwise it will continue to execute like an infinite loop.

      Scope of variable:

      Scope of variable refers to the part of the program, where it is accessible,

      The scope holds the current set of variables and their values.

      The two types of scopes are - local scope and global scope.

Local Scope:

A variable declared inside the function’s body or in the local scope is known as local variable.

     
Global scope:

A variable, with global scope can be used anywhere in the program.

It can be created by defining a variable outside the scope of any function or block.

Parameters and arguments:

Parameters are the variables used in the function definition.

Arguments are the values we pass to the function parameters.

Arguments Types:

Arguments are used to call a function.

There are primarily four types of functions namely :

Required arguments 

Keyword arguments,

Default arguments

Variable-length arguments

Required Arguments:

Required Arguments” are the arguments passed to a function in correct positional order.

The  number of arguments in the function call should match exactly with the function     definition.

Atleast one parameter to prevent syntax errors to get the required output.

Keyword Arguments:

Keyword arguments will invoke the function after the parameters are recognized by their parameter names.

The value of the keyword argument is matched with the parameter name and so, one can also put arguments in improper order (not in order).

Default Arguments:

In Python the default argument is an argument that takes a default value if no value is provided in the function call.

Variable-Length Arguments:

In some instances it is needed to pass more arguments that have already been specified.

These arguments are not specified in the function’s definition and an asterisk (*) is used to define such arguments.

These types of arguments are called Variable-Length arguments.

Composition in functions:

The value returned by a function may be used as an argument for another function in a nested manner is called composition.

 

No comments:

Post a Comment

Thanks

Featured Post

Convert WEBP to JPG free online

WEBP to JPG Converter WEBP to JPG Converter Free Converting WEBP images to JPG Introduction: Introduce the ...

Popular

ads