- Python Programming with Raspberry Pi
- Sai Yamanoor Srihari Yamanoor
- 253字
- 2021-07-02 23:48:48
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.
- 大數(shù)據(jù)技術(shù)與應(yīng)用基礎(chǔ)
- 精通MATLAB神經(jīng)網(wǎng)絡(luò)
- Hands-On Artificial Intelligence on Amazon Web Services
- Ansible Quick Start Guide
- 流處理器研究與設(shè)計(jì)
- 網(wǎng)絡(luò)化分布式系統(tǒng)預(yù)測控制
- Implementing AWS:Design,Build,and Manage your Infrastructure
- 從零開始學(xué)C++
- SAP Business Intelligence Quick Start Guide
- 中國戰(zhàn)略性新興產(chǎn)業(yè)研究與發(fā)展·增材制造
- MATLAB-Simulink系統(tǒng)仿真超級學(xué)習(xí)手冊
- AVR單片機(jī)工程師是怎樣煉成的
- 基于Proteus的PIC單片機(jī)C語言程序設(shè)計(jì)與仿真
- Practical AWS Networking
- 算法設(shè)計(jì)與分析