Functions in Python

Functions are used to store and reuse code. def statement is used to define the Functions.

Built-In or Pre-defined Functions:

max, min, print, input, float, int, etc. are Built-In Python functions.

Custom Functions:

Custom Functions are user defined functions. 

Example:

#voteEligibilityCheck function for Printing the voting ability
def voteEligibilityCheck( employeeAge ) :
    print (
        'Your age is',
        employeeAge
    );
    if employeeAge < 18 :
        print(
            'You are not eligible for voting'
        );
        print(
            'Your session is closing'
        );
    else :
        print(
            'You are eligible for voting'
        );
        print(
            'Proceed further for filling the form'
        );
#Expecting age value from the employee
strAge = input(
    'Please share your age'
);
#Converting string to integer
intAge = int( strAge );
voteEligibilityCheck( intAge );

Function with return value:

Example:

#voteEligibilityCheck function to check the voting ability
def voteEligibilityCheck( employeeAge ) :
    print (
        'Your age is',
        employeeAge
    );
    if employeeAge < 18 :
        return 'No';
    else :
        return 'Yes';
#Expecting age value from the employee
strAge = input(
    'Please share your age'
);
#Converting string to integer
intAge = int( strAge );
checkVotingEligibility = voteEligibilityCheck( intAge );
print(
    'Are you eligible to vote?',
    checkVotingEligibility
);

Leave a Reply