Posts

Showing posts with the label Python Functions

learnpython24-(Python Functions)

Image
  Python Functions In this article, you'll learn about functions, what a function is, the syntax, components, and types of functions. Also, you'll learn to create a function in Python. What is a function in Python? In Python, a function is a group of related statements that performs a specific task. Functions help break our program into smaller and modular chunks. As our program grows larger and larger, functions make it more organized and manageable. Furthermore, it avoids repetition and makes the code reusable. Syntax of Function def function_name(parameters): """docstring""" statement(s) Above shown is a function definition that consists of the following components. Keyword  def  that marks the start of the function header. A function name to uniquely identify the function. Function naming follows the same  rules of writing identifiers in Python. Parameters (arguments) through which we pass values to a function. They are optional. A colon (:) ...

learnpython24-(Python Function Arguments)

  Python Function Arguments In Python, you can define a function that takes variable number of arguments. In this article, you will learn to define such functions using default, keyword and arbitrary arguments. Arguments In the user-defined function topic, we learned about defining a function and calling it. Otherwise, the function call will result in an error. Here is an example. def greet (name, msg) : """This function greets to the person with the provided message""" print ( "Hello" , name + ', ' + msg) greet( "Monica" , "Good morning!" ) Output Hello Monica, Good morning! Here, the function  greet()  has two parameters. Since we have called this function with two arguments, it runs smoothly and we do not get any error. If we call it with a different number of arguments, the interpreter will show an error message. Below is a call to this function with one and no arguments along with their respective...

learnpython24-(Python Recursion)

Image
  Python Recursion In this tutorial, you will learn to create a recursive function (a function that calls itself). What is recursion? Recursion is the process of defining something in terms of itself. A physical world example would be to place two parallel mirrors facing each other. Any object in between them would be reflected recursively. Python Recursive Function In Python, we know that a function can call other functions. It is even possible for the function to call itself. These types of construct are termed as recursive functions. The following image shows the working of a recursive function called  recurse . Recursive Function in Python Following is an example of a recursive function to find the factorial of an integer. Factorial of a number is the product of all the integers from 1 to that number. For example, the factorial of 6 (denoted as 6!) is  1*2*3*4*5*6 = 720 . Example of a recursive function def factorial (x) : """This is a recursive functi...