Posts

Showing posts with the label python function

Functions in python - args and kwargs

Earlier we saw how to write functions in python and how to call them in our program. For those functions, the number of inputs or arguments were defined. They took fixed number of positional or keyword arguments or had a default value assigned to one or more of the arguments. 

Functions in python

Functions are sets of statements that perform a specific task. They can be called by their names, more than once in a program. Inputs can be given to functions based on which they will perform a task and can give back the result. Functions avoid same code to be written over and over again, thus, reducing the redundancy of the code. They also modularize programs by assigining one task for one function. It also makes a program easy to correct. We would need to correct the code at only one place when a function is not working as desired as compared to situation where all the places where that set of code was wirtten has to be edited. The basic syntax of a function is as follows: def func(inputs): statments 1 . . . return result For example, a function can be written that return the result of addition of two numbers. It takes two numbers, a and b as input and returns the result. It can be written in a few ways: def add_num(a,b): c = a + ...