Object Oriented
programming
Md Nagrul Islam
Lecturer
Department of Computer Science and Mathematics
Mymensingh Engineering College, Mymensingh, Bangladesh
Contents.
Python Functions
Python Functions
A function is a block of code which only runs when
it is called.
You can pass data, known as parameters, into a
function.
A function can return data as a result.
Creating a Function
In Python a function is defined using
the def keyword:
ExampleGet your own Python Server
def my_function():
print("Hello from a function")
Python Functions
Calling a Function
To call a function, use the function name followed
by parenthesis:
Example
def my_function():
print("Hello from a function")
my_function()
Python Functions
Arguments
Information can be passed into functions as arguments.
Arguments are specified after the function name, inside the
parentheses. You can add as many arguments as you want, just
separate them with a comma.
The following example has a function with one argument (fname).
When the function is called, we pass along a first name, which is
used inside the function to print the full name:
Example
def my_function(fname):
print(fname + " Refsnes")
my_function("Emil")
my_function("Tobias")
my_function("Linus")
Recursion
Python also accepts function recursion, which means a
defined function can call itself.
Recursion is a common mathematical and programming
concept. It means that a function calls itself. This has the
benefit of meaning that you can loop through data to reach a
result.
The developer should be very careful with recursion as it can
be quite easy to slip into writing a function which never
terminates, or one that uses excess amounts of memory or
processor power. However, when written correctly recursion
can be a very efficient and mathematically-elegant approach
to programming.
Recursion
In this example, tri_recursion() is a function that we have defined
to call itself ("recurse"). We use the k variable as the data, which
decrements (-1) every time we recurse. The recursion ends when the
condition is not greater than 0 (i.e. when it is 0).
To a new developer it can take some time to work out how exactly
this works, best way to find out is by testing and modifying it.
Recursion Example
def tri_recursion(k):
if(k > 0):
result = k + tri_recursion(k - 1)
print(result)
else:
result = 0
return result
print("\n\nRecursion Example Results")
tri_recursion(6)
Factorial of a number using recursion
def recur_factorial(n):
if n == 1:
return n
else:
return n*recur_factorial(n-1)
num = 7
# check if the number is negative
if num < 0:
print("Sorry, factorial does not exist for negative numbers")
elif num == 0:
print("The factorial of 0 is 1")
else:
print("The factorial of", num, "is", recur_factorial(num))
Python Program to Find the Factorial
of a Number
# To take input from the user
num = int(input("Enter a number: "))
factorial = 1
# check if the number is negative, positive or zero
if num < 0:
print("Sorry, factorial does not exist for negative numbers")
elif num == 0:
print("The factorial of 0 is 1")
else:
for i in range(1,num + 1):
factorial = factorial*i
print("The factorial of",num,"is",factorial)
Python Program to Check Prime
Number
num = 29
# To take input from the user
#num = int(input("Enter a number: "))
# define a flag variable
flag = False
# prime numbers are greater than 1
if num > 1:
# check for factors
for i in range(2, num):
if (num % i) == 0:
# if factor is found, set flag to True
flag = True
# break out of loop
break
# check if flag is True
if flag:
print(num, "is not a prime number")
else:
print(num, "is a prime number")
Python Program to Find the Sum of
Natural Numbers
num = int(input("Enter a number: "))
if num < 0:
print("Enter a positive number")
else:
sum = 0
# use while loop to iterate un till zero
while(num > 0):
sum += num
num -= 1
print("The sum is",sum)