block should be at same indent level. Block: A block within a block is called nested block. When the first block statement is indented by a single tab space, the second block of statement is indented by double tab spaces. Nested Block: Here is an example of defining a function; def ( ): value = #Assignment Statement return value #Return Statement 12th Computer Chapter - - Python Functions Now let’s check out functions in action so you can visually see how they work within a program.
Here is an example for a simple function to display the given string. def hello(): print (“hello - Python”) return Example: . . .
. Advantages of User-defined Functions . Functions help us to divide a program into modules. This makes the code easier to manage.
. It implements code reuse. Every time you need to execute a sequence of statements, all you need to do is to call the function. .
Functions, allows us to change functionality easily, and different programmers can work on different functions. Calling a Function . To call the hello() function from example . - , use the following code: def hello(): print (“hello - Python”) return (hello() Example: .
. When you call the “hello()” function, the program displays the following string as output: Output hello – Python Alternatively we can call the “hello()” function within the print() function as in the example given below. def hello(): print (“hello - Python”) return print (hello()) Example: . .
If the return has no argument, “None” will be displayed as the last statement of the output. 12th Computer Chapter - - The above function will output the following. Output: hello – Python None . Passing Parameters in Functions Parameters can be declared to functions Syntax: def (parameter(s) separated by comma): Let us see the use of parameters while defining functions.
The parameters that you place in the parenthesis will be used by the function itself. You can pass all sorts of data to the functions. Here is an example program that defines a function that helps