You are on page 1of 5

Functions:

In Python, a function is a group of related statements that performs a specific task.


Functions help break our program into smaller and modular chunks. As our program
grows larger and larger, functions make it more organized and manageable.
Furthermore, it avoids repetition and makes the code reusable.

Syntax:
def function_name(parameters):

Statements…
Statements…

Rules to define a function:


1. Keyword def that marks the start of the function header.
2. A colon (:) to mark the end of the function header.
3. One or more valid python statements that make up the function body.
Statements must have the same indentation level (usually 4 spaces).

Basic Example of Function:


Following the basic syntax above, an example of a basic Python function printing
“Hello World” to the terminal looks like this:

greeting()

 def is the built-in keyword in Python that is used to declare function.


 greeting is the name of the function.
 After the name we should give open “(“ and close “)” parenthesis where we
can give parameters.
 : indicates the start of the function body.
 Print(“Hello Students”) is the code in the function body followed by the
indentation.
 To call this function, write the name of the function followed by parentheses
as we write above “greeting()”

The output of the above program will be Hello Students.

Arguments in Python Functions


While defining a function in Python, we can pass argument(s) into the function by
putting them inside the parenthesis.

The basic syntax for doing this looks as shown below:

def functionName(arg1, arg2):

Statements…

When the function is called, then you need to specify a value for the arguments:

functionName(ValueforArg1, ValueforArg2)

Example:

Output:

Sum of Given number is 30

 First we have declared a function called “addOfTwo”. And giving two


parameters.
 Then we are adding the two parameters and storing the result in sum.
 After that we are print the sum.
 After the function we are calling the function addofTwo(10, 20) with two
arguments. This two value will be assigned to num1 and num2 and give the
result.

Global Variables
In Python, a variable declared outside of the function or in global scope is known as
a global variable. This means that a global variable can be accessed inside or outside
of the function.

Example:

name = “Python”

def language( ):

print(name, “- Inside of Function”)

language( )

print(name, “- Outside of Function”)

Output:

Python - Inside of Function

Python - Outside of Function

In the above code, we have created name as a global variable and defined a function
called language() to print the global variable to print name. Finally we call the
language() which will print the value of name.

Local Variables
Variable that are declared inside a function is called local variable. These variables
can’t be accessed outside of the function. For example

def student():

name = “Zenitsu”

print(name)
student()

Output:

Zenitsu

Here we have declared name variable inside a function called student, this name
variable can be accessed only inside of the function. if we try to access the name
variable outside the function it will Throw a NameError that ‘name’ is not
defined.

Global Constant
A variable can be declared on the module level and use it in the module as
a global variable. And we can also import it to other modules.

Mymodule.py

Greeting = 'Vanakam'

def cfunc():

global Greeting

print(Greeting)

cfunc()

nextModule.py

from Mymodule.py import Greeting

if we run nextModule.py the output will be:

Vanakam

In the Mymodule.py program we have declared a variable called Greeting and assign
it to Vanakam then inside the function and we’re adding global keyword before the
variable name then we are calling that function.
Then we are creating a new python file called nextModule.py inside that file we are
importing Greeting from Mymodule.py and print’s Vanakam

You might also like