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

Passing arguments to a function:

A function is always defined with parentheses. The parentheses are used to pass any requisite arguments to a function. Arguments are parameters required to execute a function. In the earlier example, there are no arguments passed to the function.

Let's review an example where we pass an argument to a function:

def add_function(a, b): 
c = a + b
print("The sum of a and b is ", c)

In this example, a and b are arguments to the function. The function adds a and b and prints the sum on the screen. When the function add_function is called by passing the arguments 3 and 2 as add_function(3,2) where a is 3 and b is 2, respectively.

Hence, the arguments a and b are required to execute function, or calling the function without the arguments would result in an error. Errors related to missing arguments could be avoided by setting default values to the arguments:

def add_function(a=0, b=0): 
c = a + b
print("The sum of a and b is ", c)

The preceding function expects two arguments. If we pass only one argument to this function, the other defaults to zero. For example, add_function(a=3), b defaults to 0, or add_function(b=2), a defaults to 0. When an argument is not furnished while calling a function, it defaults to zero (declared in the function).

Similarly, the print function prints any variable passed as an argument. If the print function is called without any arguments, a blank line is printed.

主站蜘蛛池模板: 师宗县| 唐山市| 万州区| 和平区| 比如县| 辽中县| 夏津县| 陵水| 益阳市| 定结县| 织金县| 沙湾县| 福鼎市| 岳阳县| 简阳市| 甘孜| 景宁| 灵石县| 临漳县| 友谊县| 建阳市| 遂川县| 滨海县| 分宜县| 普兰店市| 清流县| 偏关县| 剑阁县| 井研县| 文安县| 香格里拉县| 临漳县| 沐川县| 昌黎县| 聊城市| 临夏市| 米泉市| 陆丰市| 兰西县| 美姑县| 镇赉县|