FUNCTIONS IN PYTHON

Share via:

FUNCTIONS IN PYTHON

FUNCTION :  

  1. A function is a block of code that executes only when it is called.
  2. Data can be passed to functions as parameters.
  3. A function can return the result or output the result.

In python a function can be created or defined using a def keyword followed by the function name.

Example :

Output :

To call a function, use the function name followed by parenthesis.

When the function “function_name()” is called the function executes the code

The keyword def is used to create the function “function_name()” and to call that function the function_name() is called including the parenthesis.

We know that we can pass data into functions but how ?

The data can be passed to a function as arguments, those arguments can be used in the function.

ARGUMENTS : 

Arguments are specified after the function name, inside the parentheses. You can add as many arguments as you want, just separate them with a comma.

Let us take an example to understand it clearly.

Example : 

Output : 

In this Example data is passed to the function “section” and takena s an argument student. Every time the function section is called and some data is passed, the function prints the data. This is how arguments work.

To get a clear understanding lets have some other examples.

Examples : 

Output : 

In this example three arguments are passed in a single function call, so three parameters are taken to receive the data.

Example:

Output : 

In this example the names of two students are passed but there are three parameters so it raises an error. If there are three parameters then three types of data should be passed to the function. Meaning that if your function expects 3 arguments, you have to call the function with 3 arguments, not more, and not less.

If the exact number of arguments are unknown then arbitrary arguments can be used. Adding a “*” before the argument makes it an arbitrary argument.

This way the function will receive a tuple of arguments, and can access the items accordingly:

Example : 

Output : 

“*” indicates that it is an arbitrary argument and it can be accessed the same way as a tuple.

Arguments can be a list, tuple, dictionary or of any data type

Example : 

Output : 

RETURN VALUES : 

return keyword is used to return the values from a function

Example :    

Output : 

When returned from a function it has to be collected in the variable and that variable can be used.

TYPES OF WAYS TO USE FUNCTIONS :

No return, No arguments

No return, with arguments

Returns value, No arguments

Return value, with arguments

1. No Return , No arguments

The function has no return type and does not receive any arguments.

For example, consider the function “sayHello()” which is used to print “Hello” does not return anything and does not take any arguments.

Example 1 : 

Output : 

Example 2 : 

Output : 

2. No Return, With Arguments

The function with no return statement but taking some data as arguments.

For example, Consider a function “findRemainder()” used to find the remainder after the division of two numbers, taking those two numbers as inputs.

Example 1 : 

Output : 

Example 2 : 

Output : 

3. Returns Value, No Arguments

The function takes no arguments but returns a value after the execution of the function.

For example, Consider a function “listOfStudents()” that takes no arguments and returns the list of names of the students.

Example 1 : 

Output : 

Example 2 : 

Output : 

4. Returns Value, With Arguments

The function takes some arguments and after the execution it returns a value back.

For example, Consider a function “checkValue()” used to return true and print True if the value of the argument is less than a certain value and prints False if not.

Example : 

Output : 

PASS STATEMENT

Function definitions cannot be empty, but if you for some reason have a function definition with no content, put in the pass statement to avoid getting an error.

Example : 

No output is printed since no there is no code to execute.

 

Author    : Prudhvi Teja

LinkedIn : http://linkedin.com/in/prudhvi-teja-nagabhyru-715052224

Thank you for giving your valuable time to read the above information. Please click here to subscribe for further updates

KTExperts is always active on social media platforms.

Facebook  : https://www.facebook.com/ktexperts/

LinkedIn    : https://www.linkedin.com/company/ktexperts/

Twitter       : https://twitter.com/ktexpertsadmin

YouTube   : https://www.youtube.com/c/ktexperts

Instagram  : https://www.instagram.com/knowledgesharingplatform

 

Share via:
Note: Please test scripts in Non Prod before trying in Production.
1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading...

Add Comment