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

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.

主站蜘蛛池模板: 彩票| 新源县| 和林格尔县| 台湾省| 永吉县| 抚松县| 河西区| 芮城县| 渭南市| 邓州市| 郓城县| 无极县| 合阳县| 六安市| 神木县| 枝江市| 拜泉县| 铜梁县| 丰镇市| 轮台县| 蒙城县| 靖安县| 深水埗区| 宜丰县| 万宁市| 曲阳县| 和政县| 平谷区| 休宁县| 高陵县| 绥中县| 永川市| 义乌市| 金溪县| 瑞金市| 新密市| 贞丰县| 沐川县| 蓝田县| 蒙山县| 赤壁市|