Python Functions

A function is that part of a code, which runs itself, and produces some result when it is called. A function can be thought of as a machine, which when given an input, then produces an output, whenever it is used. 

Types of functions – 

  • User-defined Functions – A function designed by a user, according to the convenience. These can be used to perform a specific task. 
  • Built-In Functions – The pre-defined functions in python. These are used to perform generalized tasks in programs. 

Example – print() //to print something , min() //find the minimum value 

A function has different parts in Python – 

  • Function name – The name of the function is needed so that we can call the function anytime, whenever needed. 
  • Arguments – We can pass data to the function if needed, and that data is called arguments. 

Syntax to define a function in Python –

def function_name(Args){ 
//Statements 
return expression 
} 

Def is a keyword used to define a function. This tells us that this is a user-defined function. It is placed before a function name that is provided by the user to create a user-defined function. 

Function Call – 

We need to call the function when we need it. If we don’t call a function, it won’t give results. To call the function, just simply write – 

Function_name(pass the arguments if needed)

You can also call a function by this method – 

a = Function_name(pass the arguments if needed) // a is a variable, it can be used to store the result of the function if the function is returning a value. We need to store the value somewhere, to use it. 

Example – A simple program to add two numbers 

Code:

Python Code

def add_num(a,b):#function for addition
    c=a+b;
    return c; #return value
a=25  #variable declaration
b=55
print("The sum is",add_num(a,b))#call the function

Output: The sum is 80

Arguments: Information or data that we pass into a function for performing some operations are called arguments. 

Arguments are specified after the name of the function in the parenthesis, with their datatype(which specifies the type of data, to be passed into a function). We can pass multiple arguments in a single function by separating each argument with commas. We always need to pass the values in order of the arguments declared in the function itself. Sometimes, there are functions also which do not need any arguments, so we leave the space for arguments empty in the function. 

Code:

Python Code

def print_name() : #function definition 
        print("Hi , Welcome to TakeUForward!!!") 
        return #nothing in front of return statement, because the function is 
        #returning nothing 

print_name() #here no paramters are passed , but still everytime , when called , 
#this function will print the statement 
 

Output: Hi, Welcome to TakeUForward!!!

Types of Arguments:

  • Default Arguments – A function with a default argument, assumes the value of an argument itself if we do not pass anything for that function. If there are two arguments, out of which, one is a default argument, then if we pass two values for the two arguments, then the two arguments will take the passed values, but if we pass only 1 value, then the default argument will not take up the passed value, that value will be assigned to the non-default argument. If we want to have more than one default argument, then after one default argument, all the arguments to the right of that default argument must be made default.

Code:

Python Code

def sum(a, b = 5): #function definition 
    c = a + b #statemetns in which operations are to be performed 
    return c #return statement 
a = 10 
print(sum(a)) #passing the arguments , here , b will take 5 as its default value 
#if we donot pass any value to it

Output: 15′

  • Keyword Arguments – These types of arguments are passed normally in the function, but when they are called then we pass them, by writing the name of the argument and then the value so that we don’t need to remember the order of the arguments. 

Code:

Python Code

def sum(a, b): #function definition
    c = a + b #statemetns in which operations are to be performed 
    return c #return statement 
a = 10 
b = 5 
print(sum(a = 10, b = 5)) #the arguments are passed with there values written properly 

Output: 15

  • DocString – The first string after the function name is a docstring. It basically tells the user what the function is doing. Using a DocString is totally optional, but considered as good to use. 

Syntax:

print(Function_name.__doc__)//use this line in the function call and write the docstring in the function , and this line will print what the function is doing.

Code:

Python Code

def sum(a, b): #function definition 
    """Function to add two numbers""" # docstring 
    c = a + b #statements in which operations are to be performed 
    return c #return statement 
a = 10 
b = 5 
print(sum.__doc__)#the arguments are passed in the given syntax format. 
print(sum(a , b)) 

Output:

Function to add two numbers
15

  • The Return Statement – A return statement is used to exit from a function and go back to the function call and return some output to it. 

Syntax – return expression 

The return statement can return a variable, a data structure, a constant, or an expression. If none is to be returned, just write return, and this assumes that it is a void function(which returns nothing). 

For a void function that returns nothing

Code:

Python Code

def print_name() : #function definition 
    print("Hi , Welcome to TakeUForward!!!") 
    return #nothing in front of return statement, because the function is returning 
    #nothing 
print_name() #here no parameters are passed , but still everytime , when called , 
#this function will print the statement 

Output: Hi, Welcome to TakeUForward!!!

For a function that returns something –

Code:

Python Code

def sum(a, b ): #function definition
    c = a + b #statements in which operations are to be performed 
    return c #return statement 
a = 10 
b = 5 
print(sum(a, b)) #passing the arguments. 

Output: 15

Passing Functions – 

There are two different ways to pass a function – 

  • Pass by Value 
  • Pass by Function 

But in Python, The arguments are passed by reference. Whenever we pass a variable to the function, a new object is created. 

Special thanks to Ishita Dhiman for contributing to this article on takeUforward. If you also wish to share your knowledge with the takeUforward fam, please check out this article