Hello Friends,
I am Chakrapani Upadhyaya, a full stack engineer.
In this article we will learn about function in python.
What is function?
A function is simply a “piece” of code that you can use over and over again, rather than writing it out multiple times. Functions enable programmers to break down or decompose a problem into smaller pieces, each of which performs a particular task.
Python interpreter has a number of built in functions that are always available with all the versions of the python
- abs()
- lower()
- upper()
- round()
- len()
- print()
- input() etc...
And good thing is Python allow us to create user defined functions.
We use "def" keyword to create the function in python.
def <<function name>>: do something(return or not return any value)
Note : As there is no braces in python, so we need to take care about indentation to tell the specific line of code is part of that function, class, loop, if else
Here is the simple function example
create a file called : my_function.py
def string_length(name): return len(name) name_length = string_length('chakrapani') print(f'the length of the given name is {name_length}')
Output:
the length of the given name is 10
The above function help us to get the length of the given string, we have passed string "Chakrapani" to the method whose length is 10.
Conclusion:
Function in is a reusable block of code that makes a program easier to understand, test and can be easily modified without changing the calling program.
That's it for this article, see you in next article until then take care bye...