I wanted to understand the Functions in Python. There are various built-in functions in python. But we may come across situations where we need a function which we can use as per our need and can call it whenever we need it. We call these types of functions User-defined Functions. It organises the program and keeps it concise.
Functions are useful in cases where we need the same code again and again. We can call the functions with to require arguments and we will get the result.
In the above function, we can see we are defining a function hello_fun, which print “Hello Function!”. It took the def keyword to define a function. and print itself a function that prints the results of the function.
Docstrings are the most useful thing in the functions. It helps us to understand the functions like, what are its arguments and parameters and what are the results we are expecting from the Function. Docstring is written within three quotes.
In the Function, we add two numbers. This function will always add the two values i.e., 4&5 and returns 9.
return(Python is the case—sensitive) is another keyword uniquely associated with functions. When Python encounters a return statement, it exits the function immediately.
Function with Argument.
We can see that it take n as an argument and return the square of n, we can take any number as the value of n.
Functions with more than one Argument:
In the above example, we can find clearly that we have three arguments and we are taking the least of three given numbers.
In the function we are taking radius first because python takes non-keyword arguments first then it takes the keyword arguments. In the return statement, we can find the pie at first as it is an example of a positional argument. we can change the values of default arguments according to our needs.
If you don’t know exactly how many arguments we need, then we can use *args and **kwargs keywords.
The above is the example of the Args and Kwargs.
Pass keyword acts as a placeholder.
Click here to connect on linkedIN