Function In Python Program
What is Function?
In Python, there are functions. This function is used to break a program that has a long and complex command into parts consisting of simpler commands. The benefits of using your own functions are to make the code neater and more structured, minimize errors, the code can be used in other parts, code generation is faster, and makes team work easier.
In writing function names, there are writing rules like not using the default function names from Python, the first letter is the letter of the alphabet or underscore character, doesn’t contain spaces, camel cases, makes function names according to the work of the contents of the function.
The function is used with ‘def’ keyword and to call the function, just write the function name in the code, for example :
def function_name():codecodecode
Functions can have parameters and arguments. Parameters and arguments are two different things but they are both input for a function. Parameter for that input value when a function is defined and the argument for that input value when a function is called.
Also, if you are already using functions in Python, there will be local variables and global variables. Each has its own characteristics. If the local variable is created in the function itself, its value can change if it’s inside the function, and cannot be accessed by other command codes outside the function.
Meanwhile, if global variable of course the opposite of a local variable, such as not being created in a function, its value doesn’t change because it operates inside a function, and can also be accessed by all command codes and functions in your program.
Get Asma Syafira’s stories in your inbox
Join Medium for free to get updates from this writer.
There are also 2 kinds of functions, namely:
- Void function a function that has no return value. Because when the void function is executed, no value can be stored in a variable.
- Functions with a return value a function that has an inverse value. Because when this function is executed, there is a value that can be stored in a variable. This function is indicated by the return command at the end of the line.
name = "Asma"
def nameFunction():
name = "Putri"
print('name as a local variable on Function : ', name)
nameFuction()
print('name as a global variable : ', name)then the output will be
name as a local variable on Function : Putri
name as a global variable : AsmaStudy Cases
And now we will try how to create a function with arguments in Python.
def func(name, place):
print("Hello my name is{name}!, I'm from {place}"}With us making these arguments, if we call the function name, we can directly enter data in the order of the arguments.
func("Ellie", "California")#Output
Hello my name is Ellie!, I'm from California
If we move the data position
func("California", "Ellie")#Output
Hello my name is California, I'm from Ellie
Lol, that really doesn’t make sense. So don’t make the mistake of placing the data position in function.





