DEV Community

Cover image for Introduction To Python Functions
chrisMurimi
chrisMurimi

Posted on

Introduction To Python Functions

Here you learn about python functions, the component of a function, the syntax and how to write functions in python.

What is a function in Python?

Function in python is a group of related statements/ (lines of code) that performs a specific task. Functions help make the program more organized and manageable by breaking the program into smaller and modular chunks. Functions helps reduce repetition and make the code reusable.

def function_name(self):
   '''block of code in this function appear here'''
    pass
Enter fullscreen mode Exit fullscreen mode
  • Def is a keyword and marks the start of the function.
  • function_name is the name to the function. A function name to uniquely identify the function. Function naming follows the same rules of writing identifiers in Python.
  • self is a parameter. Parameters through which we pass values to a function. They are optional. *A colon (:) to mark the end of the function header.
  • Statements that make the function body must have same indentation level (usually 4 spaces).
  • If a functions returns anything the return function is used.
def sum(num1, num2):
'''this adds two numbers'''
    return num1 + num2
Enter fullscreen mode Exit fullscreen mode

The above function named sum receives two numbers as the parameters and returns the sum of the two numbers.

How to call a function in python?

A function can be called from another function, program or even the python prompt. To call a function we simply type the function name with appropriate parameters.

def sum(num1, num2):
'''this adds two numbers'''
    return num1 + num2
sum(4,5)
Enter fullscreen mode Exit fullscreen mode

The return statement.

The return statement is used to exit a function and go back to the place from where it was called. This statement can contain an expression that gets evaluated and the value is returned. If there is no expression in the statement or the return statement itself is not present inside a function, then the function will return the None object.

Scope and Lifetime of variables.

Scope of a variable is the portion of a program where the variable is recognized. Parameters and variables defined inside a function are not visible from outside the function. Hence, they have a local scope.

The lifetime of a variable is the period throughout which the variable exits in the memory. The lifetime of variables inside a function is as long as the function executes.

They are destroyed once we return from the function. Hence, a function does not remember the value of a variable from its previous calls.

def sum():
    x = 10 #value inside the function
    print(x)
y = 10 #value outside the function
print(y)
Enter fullscreen mode Exit fullscreen mode

In general, variables that are defined inside a function body have a local scope, and those defined outside have a global scope. That means that local variables are defined within a function block and can only be accessed inside that function, while global variables can be obtained by all functions that might be in your script.

Types of Functions in python.

There are three types of functions in Python:

  • Built-in functions, such as help() to ask for help, min() to get the minimum value, print() to print an object to the terminal,… You can find an overview with more of these functions here.

  • User-Defined Functions (UDFs), which are functions that users create to help them out; And

  • Anonymous functions, which are also called lambda functions because they are not declared with the standard def keyword.

How To Define A Function (User-Defined Functions).

The four steps to defining a function in Python are the following:

  • Use the keyword def to declare the function and follow this up with the function name.
  • Add parameters to the function: they should be within the parentheses of the function. End your line with a colon.
  • Add statements that the functions should execute. *End your function with a return statement if the function should output something. Without the return statement, your function will return an object None.

Parameters vs Arguments

Parameters are the names used when defining a function or a method, and into which arguments will be mapped. In other words, arguments are the things which are supplied to any function or method call, while the function or method code refers to the arguments by their parameter names.

Functions vs Methods

A method refers to a function which is part of a class. You access it with an instance or object of the class. A function doesn’t have this restriction: it just refers to a standalone function. This means that all methods are functions, but not all functions are methods.

def my_function():
    return "hello world" #this is a function.

class my_class:
     def my_method(self): # this is a method
            return "hello world"
Enter fullscreen mode Exit fullscreen mode

Top comments (0)