Professional Documents
Culture Documents
Syntax:
def function_name(parameters):
Statements…
Statements…
greeting()
Statements…
When the function is called, then you need to specify a value for the arguments:
functionName(ValueforArg1, ValueforArg2)
Example:
Output:
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( ):
language( )
Output:
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
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