0% found this document useful (0 votes)
17 views5 pages

Python Function Basics and Recursion

The document discusses various aspects of Python functions, including default parameter values, passing lists as arguments, return values, and recursion. It provides examples for each concept, illustrating how functions can handle different data types and the importance of careful implementation in recursion. Additionally, it includes an exercise related to calculating the Fibonacci sequence using user input.

Uploaded by

mekkiya.kedir
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views5 pages

Python Function Basics and Recursion

The document discusses various aspects of Python functions, including default parameter values, passing lists as arguments, return values, and recursion. It provides examples for each concept, illustrating how functions can handle different data types and the importance of careful implementation in recursion. Additionally, it includes an exercise related to calculating the Fibonacci sequence using user input.

Uploaded by

mekkiya.kedir
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Mizan Institute of Technology

Function (Part 3)

By Seid Issa(PhD in AI)


8/4/2024
Default Parameter Value
✓ The following example shows how to use a default parameter value.
✓ If we call the function without argument, it uses the default value:

Example

def my_function(country = "Norway"):


print("I am from " + country)

my_function("Sweden")
my_function("India")
my_function()
my_function("Brazil")
Passing a List as an Argument
✓ You can send any data types of argument to a function (string, number, list, dictionary etc.), and it will be
treated as the same data type inside the function.
E.g. if you send a List as an argument, it will still be a List when it reaches the function:

def my_function(food):
for x in food:
print(x)

fruits = ["apple", "banana", "cherry"]

my_function(fruits)
Return Values
✓ To let a function return a value, use the return statement:

Example def my_function(x):


return 5 * x

print(my_function(3))
print(my_function(5))
print(my_function(9))

Recursion
✓ Python 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 looping 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(…)
Example: Write a python code that takes a positive integer number from a user and sums all consecutive
lower numbers from the number up to 0.

Exercise:
1. Fibonacci sequence is a series of numbers where each number is the sum of the two preceding once.
Starting from 0 and 1. Please write a python code that prompt a user to enter a positive integer n and
calculate the nth Fibonacci sequence.

You might also like