Image

Anyone Can Code.





Image

User Defined Functions

Sammarth Kumar



What is a User Defined Function? Simply put, it is a function created by a user for themselves. Let's see how we can create these.

Creating User Defined Functions

The syntax for creating a function is as follows:

1
2
3
def <name> (<parameters>):
    <code inside function>
    return <output of function>

Important Rules for User Defined Functions

  1. Function names have the same rules as those for naming identifiers
  2. Function headers always end with a :
  3. Indentation very important and only code within the same indentation level of the function will be consiidered to be part of a function.
  4. Elements in () are known as parameters. Parameters are not always necessary and neither is a return value

To start, lets create a function to add two numbers.

1
2
3
def addition(a,b):
    result = a + b
    print(result)

Now that we have created a function, what do we do with it? In order to run the code inside a function, you must call it. To call a function, you have to use the following syntax: <function name>(<paramaters, in order>)

So lets say we want to execute our addition function on the numbers 3 and 5. To do so we would call the function as so:

1
2
3
4
5
def addition(a,b):
    result = a + b
    print(result)

addition(3,5)

Output

8

When you call a function, you tell the interpreter to execute the code inside the function.

Arguements and Parameters

What are parameters? Parameters are those placeholders created in the parentheses of a function header which can be used to store values given by a user while calling a function. Now, what are arguements? Arguements are the values given during a function call, which fill the placeholders created by parameters.

In the example above, the parameters are a and b while the arguements are 3 and 5.

Let's look at another example.

1
2
3
4
5
6
7
def subtraction(a,b):
    result = a - b
    print(result)

num1 = 2
num2 = 1
subtraction(num1,num2)

Output

1


Here, the parameters are a and b, and the arguements are num1 and num2, whose respective values are 2 and 1. As a result a and num1 correspond to the same value and hence have the same id. Same applies for b and num2.

There are two special cases we will look at for parameters:

  1. Strings as Parameters: Strings can be used as parameters as well. Though make sure that the code inside the function can be applied to strings. For example, if you have two parameters which are meant to contain strings, every statement inside the function must support strings. For example, you won't be able to multiply the parameters.
  2. Default Parameters: Default parameters are those parameters whose values have been assigned beforehand in the definition of the function.
    1
    2
    3
    4
    5
    6
    def subtraction(a,b = 2):
        result = a - b
        print(result)
    
    num1 = 2
    subtraction(num1)
    

    Output

    0
    

    Here, we gave b a value of 2 beforehand, soo we only have to give one parameter while calling the function, since one is already given. However, we can override the default parameter and add a second arguement:

    1
    2
    3
    4
    5
    6
    def subtraction(a,b = 2):
        result = a - b
        print(result)
    
    num1 = 2
    subtraction(num1,3)
    

    Output

    -1
    

Key Points for Parameters
  1. Default parameters must all be at the end. Once the first default parameter is created, all the other parameters to the right of it must be default parameters.
  2. You can use expressions, for example x+5, in your arguements.The interpreter will evaluate these expressions before calling the function.
  3. The arguements must be in the same order as the parameters.

Return Value

To let the function return a value, we use the return statement.

This statement returns a value which can be used somewhere else, and can also return a function call, which can be useful for recursion, a topic beyond the syllabus.

Flow of Control

In order to call a function, it must be defined before the function call. If we don't define a function before, we get the following error:

1
2
3
4
function()

def function():
    print(2)

Output

Traceback (most recent call last):
  File "", line 1, in <module>
NameError: name 'function' is not defined


Therefore, you must always define a function before you call it.


That's it for this tutorial! Scroll up and click on Next for the next tutorial!

For information on how you can use this information, please refer to our Terms of Use


Copyright © 2020, Q-Programming | Created by Arjun Sodhi, Balpreet Juneja and Sammarth Kumar | Developed and Designed by Sammarth Kumar