Monday, May 11, 2020

functions in python

functions is an important concept in python certification.


What is mean by functions ?

Functions = Block of code  or group of code ( you can call that function and execute ).


How to create/define/declare a function in python ?

you can do this by a def keyword


Other programming languages :

In C programming language :

1. function declaration is a different piece.

First we have to function declare and then we have to define it and these are two separate pieces in  'C' programming language.


Python :

In python, both steps are same.

by using def keyboard

we can call function with same number of arguments.

Special feature : we can also define default values.


Functions in Python:




Python functions:




In  'C' programming language


declare :   need to declare and provide a return type.

defining a function : will define the function with coding.


In Python 

declaring and defining a function in one step.

In simple everything is treated as same entity.


def functionname(optional arguments)
      suite 

Must,

indentation is must. 
suite means set of code.


we did not get any output,





when you define and declaration a function, it cannot execute by its own.

* if you want a function to be executed, you need to call the function.





How to give the optional arguments ?

name is a variable here 

and you need to pass the name.




Error state, say hello is not an empty function but your calling an empty function and that is the problem.


we dont define any datatype for name in the python  ( there is no fixed datatype in python ).


but in other programming languages, we will be specifying  int or float or something.



concatenation str.

10 is used and the datatype choose by the python is int, so concatenation is having an issue, you cannot able to  '+' operator you need to change from. 

 name     ----------------->   str(name)

value = 10 
convert to string 






what is default values to arguments ?

when you provided the default value, the benefit is you can the function with empty string.




Example for default value:





if you pass the calling function value, it will override the default value.



Lambda function 

Lambda functions in python, one of the excellent feature ( anonymous function ).

lambda arg1,arg2  :  arg1 + arg2 

( In the same line, we have to give ).
( this function will get created ).
( with out using function name we created the function ).






































No comments:

Post a Comment