pu71n@pu71n.github.io:/$ cat Python - Functions


A function is like a mini-program within a program
Function Syntax :

**def** functionName(SomeArguments)
	>Code 
	>Code
	>Code

functionName(SomeArguments)  <- **Function Call**


When the program execution reaches these calls, it will jump to the top line in the function and begin executing the code there. When it reaches the end of the function, the execution returns to the line that called the function and continues moving through the code as before.

def statements with Parameters

A parameter is a variable that an argument is stored in when a function is called. One special thing to note about parameters is that the value stored in a parameter is forgotten when the function returns. We will discuss this later.

Return Values and return Statments

When you call the len() function and pass it an argument such as ‘hello’, the function call evaluates to the integer value 5, which is the length of the string you passed it. In general, the value that a function call evaluates to is called the return value of the function. When creating a function using the def statement, you can specify the return value should be with a return statelent. A return statement consists of the following :

When an expression is used with a return statement, the return value is what this expression evaluates to.

Let’s take an example :

The None Value

In Python there is a value called None, which represents the abscence of a value. None is the only value of the NoneType data type. (Other programming languages might call this value null, nil or undefined.) Just like the boolean True and False values, None must be typed with a capital N. This value-without-a-value can be helpful when you need to store somethin that won’t be confused for a real value in a variable. One place where None is used is as the return value of print(). The print() function displays text on the screen, but it doesn’t need to return anything like for example len(). But since all function calls need to evaluate to a return value, print =() returns None.
Behind the scenes, Python adds return None to the end of any function definition with no return statement.

Keyword Arguments and print()

Keyword arguments are often used for optional parameters. For example, the print() function has the optional parameters end and sep to specify what should be printed at the end of its arguments and between its arguments, respectively.

Similarly, when you pass multiple string values to print(), the function will automatically seperate them with a string space but you could replace the default seperating string by passing the sep keyword argument.

Local and Global Scope

A local scope is created whenever a function is called. When the function returns, the local scope is destroyed, and these variables are forgotten. Scope matter for several reasons:

The Global Statement

If you need to modify a global variable from within a function, use the global statement. It tells Python that this variable in this function refers to the global variable, so don’t create a local variable with this name. Here’s an example :

Excecption Handling

Errors can be handled with try and except statements. The code could potentially have an error is put intry clause. The program execution moves to the start of a following except clause if an error happens. Here’s an example:

When code in a try clause causes an error, the program execution immeditly moves to the code in the except clause. After running that code, the execution continues as normal. Note that once the execution jumps to the code in the except clause, it does not return to the try clause. Instead it just continues moving down as normal.

Enough for today, stay tuned for the practice project. <3 pu71N 4lW4Y2 pu7 1n.