Questions

How do you Define a Function in Python?

It is pretty simple to define a function in Python. The following points need to be kept in mind while defining a Python function:

  1. Keyword “def”: In Python, you need to use the keyword “def” to specify the function.
  2. Function name followed by parenthesis “()”: After the keyword “def”, you need to write the name of the function. The naming convention rules for naming it are the same as rules for Python identifiers. A good practice is to name the function keeping in mind the task it is supposed to do.
  3. Input parameters: The arguments or the input parameters have to be mentioned within the parenthesis following the function name.
  4. A colon after the parenthesis: A colon will be following the parenthesis where arguments are entered. It marks the end of the function header. The colon precedes the code block and is indented. 
  5. Docstring: It is an optional section that documents what the role of the function is. It enhances readability if a user looks at his code after a long duration.
  6. Function Body: It comprises one or more Python statements. Essentially, these are valid statements that have the same indentation.
  7. Return statement: It is an optional statement. The return keyword can be used by the Python function to return a value.

Sample syntax of a Python function

                    

def understand_func(name):

    """

    The docstring section can tell information about the function.

    """

    print("Hello, " + name + ". Hope you are doing fine!")

Related Questions

Related Topics

Share with friends

Customize your course in 30 seconds

Which class are you in?
5th
6th
7th
8th
9th
10th
11th
12th
Get ready for all-new Live Classes!
Now learn Live with India's best teachers. Join courses with the best schedule and enjoy fun and interactive classes.
tutor
tutor
Ashhar Firdausi
IIT Roorkee
Biology
tutor
tutor
Dr. Nazma Shaik
VTU
Chemistry
tutor
tutor
Gaurav Tiwari
APJAKTU
Physics
Get Started

Leave a Reply

avatar
  Subscribe  
Notify of

Customize your course in 30 seconds

Which class are you in?
No thanks.