DEV Community

Cover image for Functions In Python
Introschool
Introschool

Posted on • Updated on

Functions In Python

Subscribe to our Youtube Channel To Learn Free Python Course and More

Functions

What are the Functions?
Till this point, you have seen the built-in functions like print(), type(), etc. Functions are a specific set of code statements that are designed to perform a specific task. You can think of functions as a machine which is designed for a specific task. It can or can not take inputs and gives the desired output on the base of its definition.

Image description
How to Define a Function?

# Function
def function_name(arguments):
    '''
    Body of the function.
    '''

Enter fullscreen mode Exit fullscreen mode

*A def keyword is used to create a function.
*If arguments are needed in the function you can give them
inside the parenthesis( ).
*Functions Arguments: Function arguments are also called *parameters. These parameters are like placeholders that you
can use inside a function. You can give as many parameters
as needed in the function.
*Inside the body of the function, you can write statements
and expression that gives a function an ability to perform
the given task.
*You can also give a function description inside a
docstring. Docstring(‘’’ ‘’’) is basically to describe what
the function does.
*A function can have an optional return statement. A return
statement is used to give output value.

# Let's create a function that can take two numbers and gives the sum of those number as an output.

def add(a,b):
    '''
    Function to add two numbers
    '''
    sum = a + b
    return sum


# Create a function which takes a name as an argument and say Hello to that name.

def say_hello(name):
    print(f'Hello {name}')
Enter fullscreen mode Exit fullscreen mode

*The function add takes two numbers and return the sum. In
this function return statement is used.
*The function say_hello takes a name as an argument which
would be a string data type and print the message saying ‘hello ’. In this function, there is no need to use a
return statement.

Calling a Function

Now you know how to define a function, but to use a function in your code you have to call a function. Let’s see how you can call a function.

# Calling a function
add(4,5)
# Output: 9

add(9,10)
# Output: 19

say_hello('Kumar')
# Output: Hello Kumar
Enter fullscreen mode Exit fullscreen mode

Global and Local Variables
You know what a variable is but when you write a function it’s really important to know the difference between global and local variables.

Global Variables
Global variables are the variables which have global scope. It means that global variables are visible throughout the program.

Local Variables
Local variables are the variables which have local scope. Local variables are defined inside the function. It means that they are visible only inside the body of the function. You can not access them outside of the function.

# Global and Local Variables 

x = 2

def func1():
    print(a)

func1()
# Output: 2
'''
Here you can see that x is a global variable that's why you can access this inside a function. 
# Global and Local Variables 

x = 2

def func1():
    print(a)

func1()
# Output: 2
'''
Here you can see that x is a global variable that's why you can access this inside a function. 
'''

def func2():
    y = 'string'
    print(y)


print(y)
'''
Output:
NameError: name 'y' is not defined

This code gave an error because in the global scope there is no variable y and here we are accessing the variable y in the global scope.
''' 


func2()

# Output: string
'''
But if you call function func2 the output will be 'string', because y is a local variable inside the function func2 so this function has access to the variable y.
'''
Enter fullscreen mode Exit fullscreen mode

You can give the same name to the global and local variable, Python interpreter will treat them as a different variable because they are defined in a different scope.

msg = 'Hello World'

def greet():
    msg = 'Hello Python'
    print(f'local: {msg}')


greet()
print(f'global: {msg}')

# Output
# local: Hello Python
# global: Hello World
Enter fullscreen mode Exit fullscreen mode

Top comments (1)

Collapse
 
morphzg profile image
MorphZG

Would be good if you mention that calling needs parenthesis (). You can reference function without calling if you just type the name.