Definitions, Syntax, Example, Types - Functions - Python | Problem Solving and Python Programming : Data, Expressions, Statements

Chapter: Problem Solving and Python Programming : Data, Expressions, Statements

Functions - Python

In Python, function is a group of related statements that perform a specific task.

Functions


In Python, function is a group of related statements that perform 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 code reusable.

 

Function Definitions

In Python each function definition is of the form

def name of function (list of formal

parameters):

body of function

For example, we could define the function max by the code

def max(x, y):

if x > y:

return x

else:

return y

def is a reserved word that tells Python that a function is about to be defined.

 

A function definition which consists of following components.

1. Keyword def marks the start of function header.

2. A function name to uniquely identify it. Function naming follows the same rules of writing identifiers in Python.

3. Parameters (arguments) through which we pass values to a function. They are optional.

4. A colon (:) to mark the end of function header.

5. Optional documentation string (docstring) to describe what the function does.

6. One or more valid python statements that make up the function body. Statements must have same indentation level (usually 4 spaces).

7. An optional return statement to return a value from the function.

 

Function Call

A function is a named sequence of statements that performs a computation. When you define a function, you specify the name and the sequence of statements. Later, you can “call” the function by name.

>>> type(32)

<type 'int'>

The name of the function is type. The expression in parentheses is called the argument of the function. The resultis called the return value.

 

The return statement

The return statement is used to exit a function and go back to the place from where it was called. The Syntax of return statement:

return [expression_list]

This statement can contain expression which gets evaluated and the value is returned. If there is no expression in the statement or the return statement itself is not present inside a function, then the function will return the None object.

For example:

>>> print(greet("May"))

Hello, May. Good morning!

None

Here, None is the returned value.

 

How Function works in Python?


 

Types of Functions

Basically, we can divide functions into the following two types:

1. Built-in functions - Functions that are built into Python.

2. User-defined functions - Functions defined by the users themselves.

 

Type conversion functions

Python provides built-in functions that convert values from one type to another. The int function takes any value and converts it to an integer, if it can, or complains otherwise:

>>> int('32')

32

>>> int('Hello')

ValueError: invalid literal for int(): Hello

int can convert floating-point values to integers, but it doesn’t round off; it chops off the fraction part:

>>> int(3.99999)

3

>>> int(-2.3)

-2

float converts integers and strings to floating-point numbers:

>>> float(32)

32.0

>>> float('3.14159')

3.14159

Finally, str converts its argument to a string:

>>> str(32)

'32'

>>> str(3.14159)

'3.14159'

 

Math functions

Python has a math module that provides mathematical functions.

>>> import math

This statement creates a module object named math. If you print the module object, you get some information about it:

>>> print math

<module 'math' (built-in)>

The module object contains the functions and variables defined in the module. To access one of the functions, you have to specify the name of the module and the name of the function, separated by a dot (also known as a period). This format is called dot notation.

Eg:

>>> math.sqrt(2) / 2.0

0.707106781187

 

Tags : Definitions, Syntax, Example, Types , Problem Solving and Python Programming : Data, Expressions, Statements
Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail
Problem Solving and Python Programming : Data, Expressions, Statements : Functions - Python | Definitions, Syntax, Example, Types

Related Topics

Problem Solving and Python Programming : Data, Expressions, Statements


Privacy Policy, Terms and Conditions, DMCA Policy and Compliant

Copyright © 2018-2023 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.