Create function in python | What Are The Functions ?
Table of Contents
In this article you will understand how to create functions in python with some basics examples step by steps.
The functions statement is use to declare the name, parameters and the body of function. In other words in functions are the convenient way to divided your code into useful block allowing developer to order their code. Functions make code easier , readable , reuse it and save the code time.
As you know python have many built-in functions like input function, print etc. But you can also create functions in python as you want. In python you can’t create functions without rules. So create your custom function in python by following these simple rules.
- Functions blog begin with the keyword def which use to write the function name like my_function etc. and open close parentheses f(()).
- Any parameters and argument is placed within these parentheses (()). like username, email,phone etc.
- The document string of the function is optional.
- The code block within every function start with a colon (:).
Syntax: (Create Function in PYTHON)
def function_name ( parameters ):
“docstring
statement (s)
return [ expression ]
Create Function In Python || Example # 01 : Call a function
Create function in python using IDLE Python 3.7
def print_user (user):
print (user)
return
print_user (“Hello user !”)
Create Function In Python || Pass By Reference:
All parameters in the python are passed by reference. It means if you change in any parameters within the function it will effect change the back the calling functins.
def changeme( mylist ):
“This changes a passed list into this function”
mylist.append([10,20,30,40]);
print (mylist)
return
# call changeme function
mylist = [1,2,3,4];
changeme( mylist );
print ( mylist );
Function Argument
In python you can call a function by using following types of arguments ;-
- Required Arguments
- keyword Arguments
- Default Arguments
- Verifiable length Arguments
Please note without using above you can’t access or call to any arguments.
For Example;
def print_user (user):
print (user)
return
print_user ()
In above example I tried to call function print_user () with no argument the following error will show.
keyword Argument:
Keyword argument are the related to the call function. When we use the keyword argument in function call, the caller indemnifies the argument from the parameters .python is a interpreter language it skip the argument and place them into order.
For Example:
Calling A Parameter Without Order
In python you can call a argument without order, the order of parameter doesn’t matter.
For Example :
def print_user (username ,email):
print (‘username’ , username)
print (‘username’ , email)
return
#calling the parameter
print_user (email = “info@gsstech.com” , username = “gsstech”)
Default Argument
A default argument is that argument which assign a default value in the function parameter .So when you call a argument in function it will automatically get the default value from the function.
For Example :
def user_info (username ,age = 22):
print (‘username’ , username)
print (‘age’ , age)
return;
user_info (age = 30 , username = “gsstech”)
user_info ( username = “gsstech”)
The output is :
Variable-Length Argument
In python some functions have no arguments. Some time we have a lot of functions and do not know the beforehand. In other words you may need to process the more argument while defining the functions these functions are called the Variable-Length function. We need to use these function when we don’t know the size of input or use in API. In python we can create a function which accept the any amount of argument.
Using Many Arguments With *args
Here we create a function of *args
#python program to example
# *args for variable number of argument define
def myfunc(*argv):
for arg in argv:
print (arg)
myfunc (‘honda’, ‘toyota’, ‘bmw’,’audi’)
Pingback: Introduction Of Object Oriented Programming (OOP) In Python 3.7 - GSS TECHNOLOGY
Thank you, I have recently been searching for information about this topic for
ages and yours is the best I’ve came upon so far.
However, what about the bottom line? Are you positive in regards to the supply?
Pingback: Free PHP MySQL POS With Source Code 2021 | GSS TECHNOLOGY