This content originally appeared on DEV Community and was authored by Tito
What's of Function ?
Function can simply be defined as a sequence of statements that execute in a certain order.For function to work it has to be called or invoked,by simply using the function’s name with parentheses.
There are three types of functions in Python:
1.Built-in functions : These are functions that come preloaded in python.You only need to call them.Examples: help(),print() , min()
2.User-Defined Functions (UDFs):These are functions created by the user to avoid repetition of code.In this blog,We will majorly focus on this type of functions.
3.Anonymous functions: which are also called lambda functions because they are not declared with the standard def keyword.
1. User-Defined Functions
In python,UDFs are defined using a keyword "def"
Syntax of a function
def function_name(arguments):
write your code here
print()
Note: A function can take an argument(1 argument or more) or no argument.You can add as many arguments as you want, just separate them with a comma.Further discussion on argument will be discussed on a different section of this blog.
Example 1 : Create a function with no argument
#Function with no argument
def Greetings():
print('Hello ,Merry Christmas')
Greetings()
Example 2 :Create a function that multiply any number passed to it by 10 and takes one argument
def Multiply_By10(num):
num = num * 10
print(num)
#calling the function
Multiply_By10(7)
Result: 70
For more examples on arguments , check out this Notebook.
Before proceeding, you will realize , in some context ,they will use the name Argument and parameters interchangeably.
What's the difference between Parameters and Arguments ?
Both are used in the function, but a parameter may refer to a variable listed inside the parentheses in the function definition . For Example 2 , "num" is a parameter.
Now, an argument may be defined as the value that is sent to the function when it is called. For example 2 the value "7" is an argument
Type of Arguments
1.Default arguments: These are arguments with default values assigned to them during function definition. We assign a default value to an argument using the assignment operator in python(=).
When calling such a function without passing any argument it will use the default value. Example:
def Say_Name(user="Kim"):
print(f'Welcome back home,{user}'
Say_Name()
#This will return
Welcome back Home ,Kim
NB:For all types of arguments example codes, check out this Notebook on Google Colab
2.Keyword arguments
3.Positional arguments
4.Arbitrary positional arguments
5.Arbitrary keyword arguments
2. Anonymous functions AKA Lambda function
Lambda function is a function that take any number of arguments, but can only have one expression.We don't assign names to lambda functions and can be used inside other functions.
Syntax
lambda arguments : expression
Example
multiply_by10 = lambda x: X *10
print(multiply_by10(69))
#Results
690
THANK YOU READING.
Follow for more post
This content originally appeared on DEV Community and was authored by Tito
Tito | Sciencx (2021-07-26T20:54:44+00:00) Understanding Functions In Python. Retrieved from https://www.scien.cx/2021/07/26/understanding-functions-in-python/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.