Recent Posts

Functions , Parameters and Keyword arguments


Functions
A function is a block of organized, reusable code that is used to perform a single, related action. Functions provide better modularity for your application and a high degree of code reusing.
As you already know, Python gives you many built-in functions like input() and print(), etc. but you can also create your own functions. These functions are called user-defined functions.

Defining a Function

You can define functions to provide the required functionality. Here are simple rules to define a function in Python.
  • Function blocks begin with the keyword def followed by the function name and parentheses ( ( ) ).
  • Any input parameters or arguments should be placed within these parentheses. You can also define parameters inside these parentheses.
Let's check the def funtion..You must do that first.




  1. Define the function first
  2. 2 lines break
  3. Make sure the code is incorrect order
  4. Call the function


Parameter

A function or procedure usually needs some information about the environment, in which it has been called. By using these parameters, it's possible to use all kind of objects from "outside" inside of a function. 
in other words, parameters are how to pass the information to the function. Inside the parenthesis, we will put the information(parameters), like a local variable



We can add some more  values, so we are reusing the greet_visitor function





We can add a second paramenter

Keyword argument

In Python the position and order matters. For example, the position of last name is not showing up so we can get an error


But the order  in positional argument matters



But with Keyword argument, the position doesn't matter. Let's see an example..



Beside this keyword arguments helps improve how easy we can read our code.
For example, this doesnt explain much for the code reader

But this does

If you have numerical values, make sure you use keyword argument, because they always come first than positional argument. You can not put a keyword argument with positional argumet. Look for the error...


If you need to put both of them, put the positional argument first, and then the keyword argument in a statement





Functions , Parameters and Keyword arguments Functions , Parameters and Keyword arguments Reviewed by ohhhvictor on May 08, 2019 Rating: 5

No comments:

Facebook

 photo imagen120.jpg
Theme images by 5ugarless. Powered by Blogger.