Old stories deleted and kept here 5
Get Rid of Doubts in Python Function Once and For All

Python has some in-built functions. But, users can also define some functions. A function contains some statements and performs a specific task. Let us take a look at the syntax, examples, and explanations about classes in python.
Def keyword in a python function
Def is a keyword in python. Whenever you define a function in python, it starts with ‘def’ keyword.
Keywords in python
Python has some keywords like def. You cannot use the python keywords as names of any other variables, functions, etc.
Like, we can write
a=12
Here, a is an integer variable with a value of 12.
But, you should not write
def=12
See what happens if you try to use ‘def’ as a variable name. It shows an error.
Now you define a function in python.
Take a look at what I have done here.
Name of the python function
GreetFunction is the name of the python function.
To define a python function, you have to follow those rules which apply to python variables.
Parameters or arguments in a python function
What appears beside the function name in brackets is the argument or parameter. Here, name is the parameter.
You could use any other parameter name to get the same results.
In the above example, I have replaced x as the argument instead of name. We get the same results.
Can you leave the argument in a python function empty?
Let us check out.
So, we cannot leave an argument empty.
What if we don’t add the brackets () beside the function name?
We get an invalid syntax.
A common mistake in jupyter notebook about python functions
When you define a function, keep it in a separate kernel. Run it then call it.
GreetFunction(x+’is a python programmer’) means we call the function.
Do not call the function in the same kernel.
Python function header
The line which contains def is the function header.
Colon (:) in python function
To mark the end of the function header, you put a colon.
Four spaces in python function body statements
Notice that after the function header, each line has four spaces to indent it.
We used four spaces to indent even when we called the function.
An example of a python function using the return statement
In the above example, I had used no return statement. So, the python program did not return any value. Or, the return value of the above example was none.
We will write an absolute value function in python
What is an absolute value function in python?
No matter what number you insert into the function, it will return its positive value.
If you insert (-8) into the function as an input, the function will give you (8) as the output.
If you insert (5) into the function as an input, the function will give you (5) as an output.
If you insert (0) into the function as an input, the function will give you (0) as an output.
So, the function has a way to detect negative values and convert them into positive values. Also, the function has a way to detect positive values and output them without any change.
Let us write an absolute value function in python.
What does the function say?
The argument is x.
If x is 0 or positive then return x
Otherwise (if x is negative) then return -x.
Let me call the function.
Here, the function returns the value x or -x on the basis of whether x is negative (<0) or non-negative(>=0).
How does a python function work?
When we call a python function (abs_value(-8)), then it goes to the line immediately below the function header (if x>=0).
All the lines (till return -x) get executed.
Then the rest of the lines below the line where you called the function get executed (whatever new code you write after this).
I made a silly mistake :(
A point of confusion can be x and X. I made this mistake. Go through the code, these are some common silly mistakes. One way to avoid them is to use names like ‘num’, ‘number’, etc.
Scope of python variables
When you define a variable inside the function then it does not remain valid outside the function. It has no scope or meaning when the function does not get executed.
Let us take a look at it.
Although we defined x after the function, when we called the function again, it retained its variable character in the function.
Loved what you read? Follow me on Medium. Visit my website.
Email me at subarnacreative@gmail.com.
Comments
Post a Comment