Net-informations.com
SiteMap  | About    

Python Functions

What is a Function?

Function is one of basic concepts in programming language. Functions are "self contained" modules of code that performs a distinct service. It is a mathematical concept that groups a collection of operations into a useful identity, which simplifies other math expressions. A function is used to segregate different functionalities of a program and often combined to perform more complicated tasks. Most programming languages come with a pre-written set of functions that are kept in a library. These functions used to remove repetitiveness thereby resulting in lesser lines of code, ease of use and better maintenance. All programming functions have input and output. The function contains instructions used to create the output from its input. You can also write your own functions to perform specialized tasks .

Python Functions

A function is defined by using the block keyword "def" , followed by the function's name, followed by a set of parentheses which hold any parameters the function will take and ending with a colon. Next follows the block of statements that are part of this function. Functions may return a value to the caller, using the keyword- 'return' .

example

Here function is now fully defined, but if we run the program at this point, nothing will happen since we didn't call the function. So, outside of the defined function block, let's call the function with sayHello()

example

output

Python Function Parameters

A parameter is the variable which is part of the method’s signature (method declaration). Parameters are specified within the pair of parentheses in the function definition, separated by commas. When we call the function, we supply the values in the same way.

example

output

example

output

Default Argument Values

We can also provide default values for function parameters in Python in case the user does not want to provide values for them. This is done with the help of default argument values. The default value is assigned by using assignment (=) operator.

example

output

The main advantage of default argument is that we can give values to only those parameters to which we want to, provided that the other parameters have default argument values.

example

output

Variable number of arguments

Sometimes the programs might want to define a function that can take any number of parameters, i.e. variable number of arguments, this can be achieved by using the stars (*). This is very useful when we do not know the exact number of arguments that will be passed to a function.

example

output

Returning a Value

Not only can you pass a parameter value into a function, a function can also produce a value. The return statement is used to return from a function. The return statement is followed by an expression which is evaluated.

example

output

Inner Functions

The main advantage of inner functions is that it protect them from anything happening outside of the function, meaning that they are hidden from the global scope.

example

output

If you try calling the findSum(10.20) function:

output

Assign functions to variables

When you assign a function to a variable you don't use the () but simply the name of the function.

example

output

Functions as parameters

There are situations we have to pass Functions as parameters to other functions.

example

output









net-informations.com (C) 2017    Founded by raps mk
All Rights Reserved. All other trademarks are property of their respective owners.