Python for Hacking | Part 3 | concept of Functions

Python for Hacking | Part 3 | concept of Functions

In this post of the series of Python for Hacking, we are now going to discuss the concept of Functions and how to declare them, call them and use them in a program with the help of an example.

So, let's get started...



3.1 What is a Function?

A Function in Python is simply a piece of code which runs only when that Function is called. A function helps in reducing the effort to type the same piece of code over and over again. This comes very handily when a program contains several hundred or thousands of lines of code and there is a need to type the same piece of code again and again. In that case, we will just define a function containing a piece of code and call that Function whenever, wherever (I love that song Lol) needed.

3.2 How to create and call a Function in Python?

3.2.1 Creating a Function in Python

In Python, a Function is declared using the def keyword followed by the name of the function. For example, if we want to create a very simple Function which when called will print something on the screen, it will be created as follows:

def some_function():
  print('hello python!')
  print('I love Python!')


[If you do not know how to create a file in Python, visit this link]

Note the colon ( : ) after the name of the function. It is compulsory to put a colon after the name of the function. These brackets ( ) after the name of the function will contain arguments which will be passed to the Function when it will be called. We will discuss arguments in the coming section.

Notice the spacing before the print('hello Python!') and print('I love Python!') commands above. It is necessary to put some spacing before each and every line inside a function. This spacing inside the function is necessary to be done because of this spacing before each and every line of the code, it will tell the Python interpreter that these lines of code are not the part of the actual program, rather it's the part of the function. If you'll not do the spacing before each and every line inside the Function, Python interpreter will return an error. This spacing before each line of the code inside the function is called indentation.



3.2.2 Calling a Python function

To call a function, just type the name of the function outside the function declaration and run the program. 

def some_function():
  print('hello python!')
  print('I love Python!')
  
some_function()

You'll get the following output:

3.3 What are arguments/parameters?

Argument or a parameter is the information/value which is passed to the function and is then used inside the function wherever required. 

Arguments are written inside the parenthesis in the function call. You can pass as many arguments as you want to a function. Just separate the arguments by a comma (,).

For example, if you want to pass first_name and last_name as the arguments to a function printName to print a name on the screen, then the program will be as follows:

def printName(first_name,last_name):
  print(first_name + last_name)

printName('Dishant',' Sharma')



In the above example, we are passing arguments as 'Dishant' and 'Sharma' of data type string to the function printName. Both the arguments inside the parenthesis in the function call statement are in ' ' because they are of the data type string and if you do not put them inside the ' ', Python interpreter will treat them as integer and it will give you an error because Dishant and Sharma are strings and you're trying to add them as integers.

When we will run this program, we will get the following output on our screen as shown in the image:



3.4 Default argument value

What if we forget to pass the argument/s while calling a function and the result of the function depends on it?

Well, we also have a solution for that problem in Python. If we will forget to pass arguments to a function, Python interpreter will take default argument values and will pass it to the function. That default value which interpreter passes to the function is called the Default Argument Value.

For example, 
def country(country_name= 'United States'):
   print('I live in '+country_name)

country() 


When we will run this program, we will get the following output as shown in the image below:


So basically in the above program, we didn't pass the argument to the function instead, the Python interpreter takes the default value of the argument which we defined to the argument while defining the function.

3.5 Returning a value

To return a value in Python, we basically use a keyword called return. For example, 
def sum(x):
   return x+2

sum(2)  


When we will run this program, we will get the following output on the screen:




In the above example, the function returns a value equal to 4.

Hope, you have understood this tutorial. If you had any doubt, please do ask in the comment section. Please share this article with your friends also.


Share:

No comments:

Post a Comment

Email Subscription

Enter your email address:

Delivered by FeedBurner

Wikipedia

Search results

Translate

Contact Form

Name

Email *

Message *

Featured Post

How hackers can hack your car?

How hackers can hack your car? For a considerable length of time, AUTOMAKERS and programmers have thought about a smart assault that ...

Search This Blog

Archive

Subscribe

Comment

Popular Posts

Recent Posts