Create a function in Python

erizky ningtya
4 min readApr 6, 2021

In simple terms, a function or function is program code designed to complete a specific task and is part of the main program. When adapted into Indonesian, this function is also called a function.

Based on who made, the functions can be divided into 2 groups:

  • Built-In Function
  • User Defined Function

Sample code :

Output

Understanding Python Function Arguments and Parameters

Parameters are the names for function input values when the function is defined, while arguments are the names for function input values when the function is called.

Sample code

Output

While defining the say_hai() function on line 1, I wrote say_hai (name). Here, the name is a parameter.

Inside functions, parameters can be accessed as variables. Parameter name then I repulse wearing a print command as in line 2.

Because say_hai function() has one parameter, so when calling this function in line 4, we have to fill out an argument which in this example is the string “Abigail”.

When executed, the argument “Abigail” on line 4 will fill in the parameter name on line 1. So when the print command (“Hi”, name) in the say_hai () function is processed, the result is “Hi Lisa”.

Return inside Function

Sample code

Output

The rectangle’s area is 24

In line 6, the rectangle feature (8, 3) is called, and a text exists in a rectangle area:

Python’s default parameters

A parameter with an initial value, or a default value, is referred to as a default parameter. The Default Argument is another name for this function.

Output

Default Location of a Parameter

A function can have many default parameters, but no parameter without a default value can be written after the parameter with the default value.

def plus(var1 = 5, var2):return var1 + var2

When executed, an error message will appear: non-default argument follows default argument.

The error occurs because the first parameter already has a default value, so the second, third, etc. parameter must also have a default value.

Python Arbitrary Arguments (* args)

The number of arguments that cannot be calculated or fluctuated is referred to as arbitrary arguments.

This time there’s an additional for loop inside the say_hai() function. This for loop will access each tuple element and then display it with the print command. The say_hai() function can now hold 1, 3, 10 or even 100 arguments and all of them can be processed.

Also note that this time I use the parameter *name instead of *args. In references or tutorials about arbitrary arguments, the parameter name *args will often be used because it stands for arguments.

Python Arbitrary Keyword Arguments (**kwargs)

Arbitrary keyword arguments is a term for a number of named function arguments that cannot be defined or fluctuated.

The difference between arbitrary arguments (*args) and arbitrary keyword arguments (**kwargs) is in writing named arguments or named parameters.

Inside the function : continued_the_word() there is a for loop to display all the argument values that are sent. However, because the word is a dictionary, what appears are the keys of the dictionary.

If we want to display the value dictionary in a for loop, we can use the code above

--

--