官术网_书友最值得收藏!

Functions

Functions or methods are a pre-written small set of instructions that result in a specific task being performed when they are called. The functions can also be defined as a single name for a group of programming instructions written together to achieve a common task.

Taking an example, think of driving as a function. In driving, there are multiple things that need to be taken care of, such as understanding traffic signals, running a car, and driving the car in traffic and on the road.

All these tasks are grouped in a function named driving. Now, let's say we have two people, example 1 and example 2, who want to learn to drive. From a programming perspective, once we define a function, we need to call it whenever we want to perform the same set of tasks. Hence, we would call driving(example 1) and then driving (example 2), which would ensure that both people would become a driver after going through the set of instructions in the driving function.

Let us look at another example:

countries=["India","China","USA","UK"]

function hellocountry(countryname)
Return "hello " countryname

for each country in countries:
hellocountry(each country)

In the first line, we declare an array with country names as elements. Next, we define a function named hellocountry that accepts an input of countryname. In the function itself, we simply return the value of the countryname that was passed to the function as input, preceding by the work hello.

Now all that remains is to iterate through all the elements of countries and pass each countryname as input to the hellocountry function. As we can see, we called the same function for each element, and based upon the instructions declared inside the function, that specific task was now performed for each element in the array.

主站蜘蛛池模板: 康保县| 大余县| 炎陵县| 晋州市| 博乐市| 习水县| 桃江县| 永嘉县| 乌拉特前旗| 阳东县| 道孚县| 酉阳| 定结县| 昌乐县| 都江堰市| 霍邱县| 哈尔滨市| 安西县| 宁波市| 云浮市| 广河县| 乌拉特中旗| 大新县| 沧源| 澜沧| 河北省| 佳木斯市| 漳州市| 来凤县| 仁寿县| 宁陕县| 赤壁市| 灵山县| 祥云县| 香格里拉县| 奎屯市| 甘洛县| 延津县| 治县。| 扎鲁特旗| 兴化市|