Advantages of User-defined Functions in Python, Exercises of Advanced Computer Programming

This Python Function allow us to change functionality easily, and different programmers can work on different functions. The syntax to define a function is: To ...

Typology: Exercises

2021/2022

Uploaded on 08/05/2022

nguyen_99
nguyen_99 🇻🇳

4.2

(80)

1K documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
2. USER DEFINED FUNCTIONS: A function defined by programmer/user
according to their need
Advantages of User-defined Functions in Python
This Python Function help divide a program into modules. This makes the code
easier to manage, debug, and scale.
It implements code reuse. Every time you need to execute a sequence of
statements, all you need to do is to call the function.
This Python Function allow us to change functionality easily, and different
programmers can work on different functions.
The syntax to define a function is:
To define your own Python function, you use
the ‘def’ keyword before its name. And its name is to be followed by parentheses,
before a colon(:). Parenthesis may contain argument(s) i.e. parameter(s).
#Syntax of function without parameters
def functionname( ):
"function_docstring"
Statement1
.
.
#Syntax of function without parameters
def functionname( arg1,arg2,.... ):
"function_docstring"
Function statement
.
.
return (expression)
Example:
# A simple Python function to check
# whether x is even or odd
def evenOdd( x ):
if (x % 2 == 0):
print "even"
else:
print "odd"
# Driver code to call the function
evenOdd(2)
evenOdd(3)
Function Parameter(s):
A functions has two types of parameter(s):
1. Formal Parameter: Formal parameters are written in the function prototype
and function header of the definition. Formal parameters are local variables
which are assigned values from the arguments when the function is called.
pf2

Partial preview of the text

Download Advantages of User-defined Functions in Python and more Exercises Advanced Computer Programming in PDF only on Docsity!

2. USER DEFINED FUNCTIONS: A function defined by programmer/user according to their need

Advantages of User-defined Functions in Python

  • This Python Function help divide a program into modules. This makes the code easier to manage, debug, and scale.
  • It implements code reuse. Every time you need to execute a sequence of statements, all you need to do is to call the function.
  • This Python Function allow us to change functionality easily, and different programmers can work on different functions.

The syntax to define a function is: To define your own Python function, you use

the ‘def’ keyword before its name. And its name is to be followed by parentheses, before a colon(:). Parenthesis may contain argument(s) i.e. parameter(s).

#Syntax of function without parameters def functionname( ): "function_docstring" Statement . . #Syntax of function without parameters def functionname( arg1,arg2,.... ): "function_docstring" Function statement . . return (expression)

Example:

A simple Python function to check

whether x is even or odd

def evenOdd( x ): if (x % 2 == 0): print "even" else: print "odd"

Driver code to call the function

evenOdd(2) evenOdd(3)

Function Parameter(s):

A functions has two types of parameter(s):

1. Formal Parameter : Formal parameters are written in the function prototype

and function header of the definition. Formal parameters are local variables

which are assigned values from the arguments when the function is called.

2. Actual Parameter : When a function is called , the values that are passed in

the call are called actual parameters. At the time of the call each actual

parameter is assigned to the corresponding formal parameter in the function

definition.

Example :

def ADD(x, y): #Defining a function and x and y are formal

parameters

z=x+y

print("Sum = ", z)

a=float(input("Enter first number: " ))

b=float(input("Enter second number: " ))

ADD(a,b) #Calling the function by passing actual parameters

In the above example, x and y are formal parameters. a and b are actual

parameters.

Calling the function:

Once a function is defined, we can call it from another function, program or

even the Python prompt. To call a function we simply type the function name

with appropriate parameters.

Syntax:

function-name(parameter)

Example:

ADD(10,20)

OUTPUT:

Sum = 30.