Py07: Function

A function is a set of code that performs a task and returns a result. The function takes in parameters as data, process the data, and returns a result as information. In order to use a function, it needs to be called.

Introduction

Python comes with built-in function such as print(), range(), input etc.

User-defined functions are written by ourselves in an application. A function is defined using the def keyword.

def functionName(parameters):
    statement(s)
    return expression

A function definition block begins with the def keyword and is followed by the function name and parentheses containing parameters and ends with a colon ':' sign. The parameter is separated by a comma.

The second line contains the statements of the function. The statements are indented and these form the block known as the function body. One of the statements in the function body is the return statement returns back expression to its caller.

def add(num1, num2):
    total = num1 + num2
    return total

Calling Function

A function is called to execute them. The syntax is simply:

functionName(arguments)

arguments are separated by a comma and in the exact order as a function specified in the function definition.

Required argument

Arguments are required for all parameters.

The argument is assigned to parameters according to sequence order. The arg1 is assigned to param1, arg2 is assigned to param2, and so on.

Example

def add(num1, num2):
    total = num1 + num2
    return total

answer = add(2, 3)
print("Total", answer)

Output

Total 5
> 

The add() function is called with 2 and 3 as arguments. Value 2 will be stored in num1 and value 3 in num2 in the parameter. By replacing the value with an addition expression the values are added and assigned to the result. Finally, the result is returned to the caller.

Default argument

To give a default value to the parameter.

def functionName(param1, param2 = value):
    statement(s)

Here, param1 is a required argument and param2 is a default argument. All parameters can be default arguments. After a parameter is defined as a default argument, all the parameters that follow also must be the default arguments.

We can call this function as we normally do and we also can call without the default argument because the default value will be used.

def add(num1, num2 = 5):
    total = num1 + num2
    return total

answer = add(2)
print(answer) # 7

Output

Total 7
> 

When add() function is called the second argument is not given. In this case, num1 is 2 and num2 is 5, so the output will be 5.

When the second argument is given, the value default value is replaced by the given value and used in the calculation.

Keyword argument

When calling a function, arguments must follow the order of the parameter in the function definition. By using keyword argument we don't have to worry about the order anymore. When making the call we write the parameter name and its value.

functionName(param1 = value1, param2 = value2)

after the keyword argument is used, all other arguments that follow also must be a keyword argument. Trying to use keywords that are not defined as parameters also causes errors.

For example

def add(num1, num2):
    total = num1 + num2
    return total

answer = add(num2 = 4, num1 = 2)
print("Total ", answer)

Output

Total 6
>

the first argument set the value for the second parameter num2 and the second argument set the value for the first parameter num1.

Variable-length argument

To pass a length of argument not specified in a function definition. Unlike the required argument and default argument, variable-length arguments don't have a specific name.

An asterisk (*) is used

def functionName(*tuple_name):

For example,

def showNumbers(*nums):
    print(nums)
    for a in nums:
        print(a)

showNumbers(1, 2, 4)

Output

(1, 2, 4)
1
2
4

A post by Cuber

Comments

Popular posts from this blog

Drawing Simple Pie Chart

VB.net connecting to SQL Server

VB.net