FUNCTIONS IN PYTHON
FUNCTION :
- A function is a block of code that executes only when it is called.
- Data can be passed to functions as parameters.
- 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 :
1 2 3 |
def function_name(): print("Example of function creation") function_name |
Output :
1 |
Example of function creation |
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 :
1 2 3 4 5 6 |
def section(student): print(student) section(" James") section("Haaland") section("Mbappe") |
Output :
1 2 3 |
James Haaland Mbappe |
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 :
1 2 3 4 |
def section(studentl,student2,student3): print(student1+" "+student2+" "+student3|) section("James", "Haaland", "Mbappe") |
Output :
1 |
James Haaland Mbappe |
In this example three arguments are passed in a single function call, so three parameters are taken to receive the data.
Example:
1 2 3 4 |
def section(studentl,student2,student3): print(studentl+" “+student2+" "+student3) section("James", "Haaland"|) |
Output :
1 2 |
section( "James", "Haaland") TypeError: section() missing 1 required positional argument: 'student3’ |
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 :
1 2 3 4 |
def section(*students): print("The newly joined student is : "+students[1]) section( "James", "Haaland", "Mbappe") |
Output :
1 |
The newly joined student is : Haaland |
“*” 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 :
1 2 3 4 5 6 |
def section(students): for 1 in students: print(i) students = ["James", "Haaland", "Mbappe"] section(students) |
Output :
1 2 3 |
James Haaland Mbappe |
RETURN VALUES :
return keyword is used to return the values from a function
Example :
1 2 3 4 5 |
def addition(a, b): return a+b res = addition(5,6) print("Addition of 5 and 6 is : "+str(res)) |
Output :
1 |
Addition of 5 and 6 1s : 11 |
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 :
1 2 3 4 |
def sayHello(): print("Hello") sayHello())| |
Output :
1 |
Hello |
Example 2 :
1 2 3 4 |
def operations(): - print("Sum of 4 and 5 is :",445) print("Difference between 5 and 4 is :|",5-4) operations() |
Output :
1 2 |
Sum of 4 and 5 1s : 9 Difference between 5 and 4 1s : 1 |
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 :
1 2 3 4 5 |
def findReaminder(numl, num2): remainder = numl%num2 print( "remainder of {} and {} is {}".format(numl,num2,remainder)) findReaminder(14,3) |
Output :
1 |
remainder of 14 and 3 1s 2 |
Example 2 :
1 2 3 4 5 6 |
def printAnimals(animals): for 1 in animals: print(i) animals = ['lion', 'tiger', 'wolf', 'elephant'] printAnimals(animals) |
Output :
1 2 3 4 |
lion tiger wolf elephant |
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 :
1 2 3 4 5 6 7 |
def 1istOfStudents(): students = ['elisa', 'higuan', 'thomas'] return students names = listOfStudents() for i in names: print(i) |
Output :
1 2 3 |
elisa higuan thomas |
Example 2 :
1 2 3 4 5 6 |
def classStudents(): studentDetails = {'elisa’':1, 'higuan':2, 'thomas':3} return studentDetails register = classStudents() print(register) |
Output :
1 |
{'elisa': 1, 'higuan': 2, 'thomas': 3} |
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 :
1 2 3 4 5 6 7 8 9 10 |
def checkValue(num): if num < 10: return True return False check = checkValue(6) if check == True: print{"True") else: print("False") |
Output :
1 |
True |
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 :
1 2 3 |
def passExample(): pass passExample() |
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