Python Procedure and Function
In this lecture, we are going to learn about the Python Function and learn how to create own functions in Python.
It is a method to create their own function to use in the program. We can also define more than one function in a single program.
How to create their own Functions?
We can use def keyword then function name at the end of the first line we used a colon (:).
In the procedure, the last line will return the statement to return some output of the function. Let we take an example in which sum function takes two input from the user to add.
Example:1
def add ( a , b ): # own created function return (a+b) #return the result of two value print ( add ( 5 , 6 ) ) #pass value as a arguments in own define function Output: 11
Description: def keyword is used to define own function and end with a colon (:). In the end, we can use the return statement to return the output of the function.
Now we can create a function for multiplication
Example:2
def multiple ( a , b ): #define own function for multiplication return ( a * b ) print ( multiple ( 2 , 3 ) ) Output: 6
How to make more than one function in a single program?
Example:3
def add ( a , b ) : # for addition of two value return ( a + b ) def multi ( a , b ) : # for multiplication of two values return ( a * b ) x = input (" Enter 1st number ") y = input (" Enter 2nd number ") p = add ( int(x) , int(y) ) # addition of two value q = multi ( int(x) , int(y) ) # multiplication of two value print ( " Additon of two value is: " + str (p) ) print ( " Multiplication of two value is: " + str (q) )
Description: In this program, we defined two functions first is for the addition of two numbers and second for multiplication.
Both functions get input from the user and pass to the functions as an argument.
How to use the python function as a string?
We will define a function to concatenate of strings.
Let us take an example to create a function that is used to concatenate two strings given by the user.
Example:4
def strings ( a , b ) : return ( a + b ) print ( strings (" computer " + " Science ") ) # now need to put the value as a string Output: computer science
Related Video
Video’s Credit: CS Dojo
I hope this article is very helpful in “Python function”.
In the next lecture, we will discuss “if-else statements”. If you are a new visitor to my website, You can also learn my previous lecture “Python Built-in functions“. If you have any queries about this lecture, discuss it in the comment section.