Modules in Python

Function in Python programming language

Functions In Python Programming Language

Functions allow you to put a block of codes inside it that will do certain tasks when you call it.

How To Define A Function?

Syntax

#____ProgrammerCodes.com____
def functionName():
	Code
#____ProgrammerCodes.com____

You write “def” and you write the name of the function. It is better to name the function with a name that is related to what is inside it.

Inside the function, we write our code and we can write or call another function as well.

How To Call A Function?

#____ProgrammerCodes.com____
functionName()
#____ProgrammerCodes.com____

We write its name with “()” as well as we can write print before it. It depends on your program and codes.

#____ProgrammerCodes.com____
print(functionName())
#____ProgrammerCodes.com____

Inside parentheses, we can add parameters or arguments. By including them we can personalize our function and adding multiple external values to work with the function code.

We can include them when we define the function and also when we call it but with some changes.

Example Of A Function

#____ProgrammerCodes.com____
num1 = 3
num2 = 5
def sum(num1,num2):
	print(num1 + num2)
sum(num1,num2)
#____ProgrammerCodes.com____

Result

8
How To Call A Function in python

In this example, we have assigned our variables and we define a function that will take 2 input numbers in order to do some math then it will print the sum of these numbers. We called our sum function but we give it the two arguments to complete the process.

It is important in writing code to not repeat it a lot in the program so this is why functions are important and they will help you to use the code and reuse it again in another function or to perform another task.

What Are Local And Global Variables In Python’s Functions?

If you assign a variable inside the function, you won’t be able to use it outside the function or in other functions in the program. We call this variable “Local Variable”. It is just local and can’t work outside its function.

The variables that can be called in all parts of the program are “Global Variables”.

To allow the local variable to be accessible in all program parts, we need to turn it into a global variable. We do it this way:

  1. Write “global” and then write the name of the variable.
  2. In the next line assign the variable as we always do.
#____ProgrammerCodes.com____
global a
a = 5
#____ProgrammerCodes.com____

This example when a is a local variable:

#____ProgrammerCodes.com____
def sum():
   a = "Hey"
   print(a)
sum()
print(a)
#____ProgrammerCodes.com____

Result

Hey
Traceback (most recent call last):
File "C:\Users\Admin\Desktop\test.py", line 5, in
print(a)
NameError: name 'a' is not defined
local variable in python

It will produce an error and inform that the variable a is not defined so it is not accessible outside the function.

This example when a is a global variable:

#____ProgrammerCodes.com____
def sum():
   global a
   a = "Hey"
   print(a)
sum()
print(a)
#____ProgrammerCodes.com____

Result

Hey
Hey
global variable in python

Now it works without any error.

Tutorial’s Questions:

What Is the correct syntax to define a function in Python programming language?

  • def functionName[]
  • functionName()
  • def functionName()

What Is the correct syntax to call a function in Python programming language?

  • functionName{}
  • functionName()
  • def functionName[]

Choose correct answers and write them in the comment section.

Simple Examples

If you have learned the basics of Python and you want to see some simple examples about these concepts such as numbers, for loop, function, and so on, I suggest you visit these articles below:

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *