Python Functions | Creating a operate | Parameters | Default Parameter worth | Recursion with Example


Python Functions | Creating a operate | Parameters | Default Parameter worth | Recursion with Example

FUNCTION

operate could be a block of code that solely runs once it’s referred to as.

You can pass informationcalled parameters, into a operate.

operate will come back information as a result.

Creating a operate

In Pythonoperate is outlined mistreatment the def keyword:

Example
def my_function():
print(“Hello from a function”)

Creating a operate

To decision a operate, use the operate name followed by parenthesis:

Example

def my_function():
print(“Hello from a function”)

my_function()

Parameters

Information may be passed to functions as parameter.

Parameters ar nominal once the operate name, within the parentheses. you’ll add as several parameters as you would likesimply separate them with a comma.

The following example features a operate with one parameter (fname). once the operate is termedwe have a tendency to pass on a primary name, that is employed within the operate to print the total name:

Example
def my_function(fname):
print(fname + ” Refsnes”)

my_function(“Emil”)
my_function(“Tobias”)
my_function(“Linus”)


Default Parameter worth

The following example shows a way to use a default parameter worth.

If we have a tendency to decision the operate while not parameter, 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 an inventory as a Parameter
You can send any information forms of parameter to a operate (string, number, list, wordbook etc.), and it’ll be treated because the same information kind within the operate.

E.g. if you send an inventory as a parameter, it’ll still be an inventory once it reaches the function:

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

fruits = [“apple”, “banana”, “cherry”]

my_function(fruits)

Return Values

To let a operate come back a worth, use the come back statement:

Example


def my_function(x):
come back five * x
print(my_function(3))
print(my_function(5))
print(my_function(9))

Recursion

Python conjointly accepts operate rulewhich implies an outlined operate will decision itself.

Recursion could be a common mathematical and programming conception. It means a operate calls itself. This has the good thing about which means that you simply will loop through information to succeed in a result.

The developer ought to be terribly careful with rule because it may be quite simple to slide into writing a operatethat ne’er terminates, or one that uses excess amounts of memory or processor power. However, once written properly rule may be a awfully economical and mathematically-elegant approach to programming.

In this example, tri_recursion() could be a operate that we’ve outlined to decision itself (“recurse”). we have a tendency to use the k variable because the informationthat decrements (-1) whenever we have a tendency torecurse. The rule ends once the condition isn’t bigger than zero (i.e. once it’s 0).

To a brand new developer it will take it slow to figure out however specifically this works, best thanks to establishis by testing and modifying it.

Example

Recursion Example


def tri_recursion(k):
if(k>0):
result = k+tri_recursion(k-1)
print(result)
else:
result = 0
come back result

print(“nnRecursion Example Results”)
tri_recursion(6)

Python Functions | Creating a operate | Parameters | Default Parameter worth | Recursion with Example

Have any Question or Comment?

Leave a Reply

Your email address will not be published. Required fields are marked *

Domain & free Hosting

Categories

You have successfully subscribed to myprogrammingschool

There was an error while trying to send your request. Please try again.

My Programming School will use the information you provide on this form to be in touch with you and to provide updates and marketing.