Function

Function


- A function is the block of code that is run when it is called.

- You can execute data into a function as a parameter.


Creating a Function:

- In Python, a function is defined and written by the def keyword.


Below code explanation:

- In the below code, my_greet() is a function.

- Return Nothing because we need to call the function to see the data in a function.



Calling a Function:

- To call a function, write the function name with parenthesis.

- Return the string in the print() function.

- Syntax: def function_name():
                        print("Type Something")
                function_name()



Arguments:

- Arguments are placed after the function name, inside the parentheses.

- You can add many arguments as you want, and separate them with a comma.


Below code explanation:

- In the below code, name is an argument of my_greet() function.

- Add name argument in print() function to see the arguments value and separate them with a comma.

- We call the my_greet() function with a "chirag" argument.

Syntax: def function_name(name):

                        print("Type Something", name, "Type Something")

                function_name("Type Any Name")





- If we need to return more data with one argument, then we can do that:



Number of Arguments:

- If we need to return more data with less function calling then we need to take more arguments.

- In the below code, the my_greet() function has 3 arguments and calls the function with 3 data arguments.



Error in Number of Arguments:

- We need to take the same arguments and data arguments means if we take 3 arguments then we have to take 3 data arguments otherwise python will raise an error.

- In the below code, the my_greet() function has 3 arguments but calls the function with only 1 data argument that's why it will throw an error.


Output:



Keyword Arguments:

- We can write arguments with the key = value syntax.



Below code explanation:

- In the below code, myname and friendname is an argument of friends() function.

- Add myname and friendname argument in print() function to see the arguments value and separate them with a comma.

- We call the function with key = value argument. example: name="chirag" and friendname="sanju"

Syntax: def function_name(argument1, argument2):

                        print("Type Something", argument1, "Type Something",                                     argument2)

                function_name(argument1="Type Any Name",                                                                      argument2="Type Any Name")


Default Argument Value:

- If we call the function without argument, then it use the default argument value of the function.



Below code explanation:

- In the below code, best_friend="Sanju" is an default argument value of friends() function.

- Add best_friend argument in print() function to see the arguments value and separate them with a comma.

- We call the friends() function with argument and without argument.

Syntax: def function_name(argument1="Type Any Name"):

                        print("Type Something", argument1)

                function_name("Type Any Name")
                function_name()



List as an Argument:

- You can use any datatypes for argument to a function (string, number, list, dictionary, etc.)

- If we take List as an argument:

- Return the Value in List.



Below code explanation:

- In the below code,friendship is an argument of my_friends() function.

- Add friendship argument in print() function to see the arguments value.

- We create a friends named list.

- We call the my_friends() function with list argument.

Syntax: def function_name(argument1):

                        print(argument1)

                list = ["name1", "name2", "name3"]

                function_name(list)



Return Values:

- In the below code, function return a value by using the return statement:



Below code explanation:

- In the below code, a is an argument of sum() function.

- Add a with any integr in x named variable.

- We call the function with some interger(9,19,11) arguments in print function.

Syntax: def function_name(argument1):

                        x = Any Number + argument1
                        return x 

                print(function_name("Type Any Number"))
                print(function_name("Type Any Number"))



The Pass Statement:

- If you need a function with no content for some reason then you can use the pass statement to avoid getting any error.



Comments