Functions in Python

05 min read

In certain situations, a need is raised in such a way that a particular part of the program needs to be repeated several times in various parts of the same program. In such situations, the repeated part of the program is defined as a function and the function is called whenever necessary instead of typing that bit of the program again and again. The syntax for defining a function is given as follows.

def function_name(parameters):
body of the function

functionname(paramenters); # calling the function separately


Now consider the following example to understand better,

def check():                                                    # this part of the program is not executed untill the function is called in the later part of the program. 
print('welcome') # a function names 'check' is created which can print the 2 print statements inside it, if and only if it is called elsewhere in the program.
print('to FACE Prep')

check()
print('call the function again to observe the output')
check()
Output:

welcome
to FACE Prep call the function again to observe the output welcome
to FACE Prep
POST A NEW COMMENT
     
  • Input (stdin)

    Output (stdout)


    Input (stdin)

    Your Output (stdout)

    Expected Output

    Compiler Message

    Input (stdin)

    2    3

    Your Output (stdout)

    5

    Expected Output

    5

    Compiler Message

    5

    Error