BASICS OF FUNCTIONS

Python tutorial · PySpark.in

BASICS OF FUNCTIONS PYTHON

A function in Python is a named block of structured and reusable code that is designed to perform a specific task. Functions help in organizing a program into smaller, manageable units, thereby improving readability, reusability, and maintainability of code.Functions are created using the def keyword and can be executed by calling their name.

Need for Functions

Without functions:

With functions:

General Syntax of a Function

```

HIDE

def function_name(parameters):

statements

return value

```

Important Terminologies

Term

Meaning

Function Definition

Writing the function

Function Call

Executing the function

Parameters

Variables in function definition

Arguments

Values passed during function call

Return

Value sent back to caller

Let’s understand each ……

1.FUNCTION DEFINITION

Function definition refers to the process of creating or declaring a function using the def keyword, specifying the function name, parameters, and body.

During function definition, Python does not execute the function, it only stores the function in memory.

Syntax

```

Hide

def function_name(parameters):

statements

``` Example

```

def add(a, b):

c = a + b

return c

```

2. FUNCTION CALL

A function call refers to the process of executing a function by using its name followed by parentheses ().When a function is called, control of the program shifts from the calling statement to the function body.

Syntax

```

Hide

function_name(arguments)

```

Example

```

Hide

result = add(10, 20)

```

Execution Flow

  1. Program encounters function call
  2. Control transfers to function definition
  3. Arguments are assigned to parameters
  4. Function body executes
  5. Control returns to the calling statement

3. PARAMETERS

Parameters are the variables listed in the function definition that receive values from the calling function. They act as placeholders for the data that will be passed during the function call.

Example

```

hide

def multiply(x, y):

return x * y

```

Here:

Characteristics of Parameters

Note
Parameters exist only inside the function.

4. ARGUMENTS

Arguments are the actual values supplied to a function at the time of function call. They provide the data on which the function operates.

Example

```

hide

multiply(5, 4)

```

Here:

Difference Between Parameters and Arguments

Parameter

Argument

Defined in function definition

Used in function call

Acts as a placeholder

Actual value passed

Variable name

Constant or variable

Local to the function

Exists in calling scope

Receives data

Supplies data

Defined only once

Can change for each call

5.RETURN STATEMENT

The return statement is used to send a value from the function back to the calling statement and terminate the execution of the function. If a function does not use a return statement, it returns None by default.

Syntax

```

Hide

return expression

```

Example

```

hide

def square(n):

return n * n

result = square(4)

print(result)

```

Key Characteristics of Return

Example: Multiple Return Values

```

hide

def operations(a, b):

return a+b, a-b

x, y = operations(10, 5)

```

COMPLETE FLOW EXAMPLE

```

hide

def add(a, b): # Function Definition

return a + b # Return statement

result = add(10, 20) # Function Call with arguments

```

More Python tutorials

All tutorials · Try the free PySpark compiler · Practice challenges