Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (50.2k points)
Can anyone tell me how to write a function in Python?

1 Answer

0 votes
by (108k points)

Functions or methods are tasks that you wish to perform. In Python, you can define a function or a method using the keyword ‘def.’

For example:

def add(a,b):

return a+b

add(10,20)#calling the user defined function

Output: 

30

As you can see, in the above example, I have created a user-defined function named ‘add.’ Inside the method, I have passed two arguments (i.e., parameters). Then, I defined a task inside the method, ‘a+b.’ Now, when I pass any two arguments inside the function, it will perform the same task, a+b. Thus, when I passed ‘a’ as ‘10’ and ‘b’ as ‘20,’ you can see that I got the output, 30.

If you are looking for an online course to learn Python, I recommend this Python Certification program by Intellipaat.

Browse Categories

...