Function with arguments

So far we saw a function which had no arguments, now we will learn about functions with arguments. A function can contain any number of arguments depending on the business requirement. Let's try to understand with an example:

def func(passArgument): 
print passArgument
str = "hello all"
func(str)

In the preceding example, the func function accepts one argument which has a data type string. We create a variable str with a certain string statement assigned and then we call the func function and thereby pass the value of str. Finally, the output will look something like this:

In the preceding example, what will happen if we do not pass any argument to the function?

def func(passArgument): 
print passArgument
str = "hello all"
func()

It will simply throw a TypeError as shown in the following screenshot:

Hence, it becomes mandatory to pass an argument to the function during function calling after the function has been defined to accept arguments.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.17.157.6