Python Functions Mastery Your Complete Guide for Success

A function is a block of code that performs a appointed task.

If we need to create a program to make a circle and color it. We can make two functions and solve this problem:

  1. function to make a circle
  2. function to color the shape

Partitive a complex problem into smaller lumps makes our program easy to understand and reuse.

Make a Function

Let’s make our first function.

def greet():
    print('Hello World!')

Above are the different parts of the program:

Screenshot-2024-02-26-125759

Above, we have maked a simple function named greet() which prints Hello World!

Calling a Function In python

In the upon example, we have expressed a function named greet().

def greet():
    print('Hello World!')

Suppose we run the upon code, we won’t get an output.

It’s because making a function doesn’t mean we are executant the code inside it. That means the code is there for us to use if you want to.

For use this function, you need to call the function.

Function Call

greet()

Example: Function Call In Python

def greet():
    print('Hello World!')

# call the function
greet()

print('Outside function')

Output :

Hello World!
Outside function

In the upon example, we have maked a function named greet(). Below how the control of the program flows:

Screenshot-2024-02-26-130620

Below,,

  1. So that the function greet() is called, the program’s control disposal to the function definition.
  2. All the code inside the function is evolved.
  3. The control of the program leaps to the next statement after the function call.

Function Arguments In Python

Arguments are inputs given to the function in Python.

def greet(name):
    print("Hello", name)

# pass argument
greet("John")

OutPut:

Hello John

Above, we have passed 'John' as an contention to the greet() function.

We can pass several arguments in every call, making the function re-usable and progressive.

Now lets call the function with a several argument.

greet("David")

Output :

Hello David

Example: Function to Add Two Numbers In python

# function with two arguments
def add_numbers(num1, num2):
    sum = num1 + num2
    print("Sum: ", sum)

# function call with two values
add_numbers(5, 4)

Output :

Sum: 9

In the upon example, we have maked a function named add_numbers() with arguments: num1 and num2.

Screenshot-2024-02-26-131507

The return Statement In Python

We return a value from the function by using the return statement.

# function definition
def find_square(num):
    result = num * num
    return result

# function call
square = find_square(3)

print('Square:', square)

Output :

Square: 9

s In the upon example, we have maked a function named find_square(). The function receive a number and returns the square of the number.

Screenshot-2024-02-26-132031

The pass Statement In python

The pass statement distributes as a placeholder for future code, prohibiting errors from empty code blocks.

It’s typically used where code is planned so that has yet to be written.

def future_function():
    pass

# this will execute without any action or error
future_function()  

Python Library Functions

Python take steps some built-in functions that can be right away used in our program.

We don’t requirement to make the function, we just need to call them.

Follow Python library functions are:

  1. print() - prints the string under the quotation marks
  2. sqrt() - reports the group root of a number
  3. pow() - reports the power of a number

This library functions are defined under the module. And to use them, we must comprise the module inside our program.

Now for example, sqrt() is defined under the math module.

Example: Library Function In Python

import math

# sqrt computes the square root
square_root = math.sqrt(4)

print("Square Root of 4 is",square_root)

# pow() comptes the power
power = pow(2, 3)

print("2 to the power 3 is",power)

Output:

Square Root of 4 is 2.0
2 to the power 3 is 8

Below, we imported a math module to make the library functions sqrt() and pow().

Function Arguments in Python

In computer python programming, an contention is a value that is accepted by a function.

Example 1: Function Arguments in Python

def add_numbers(a, b):
    sum = a + b
    print('Sum:', sum)

add_numbers(2, 3)

# Output: Sum: 5

In the upon example, the function add_numbers() takes two parameters: a and b. Follow the line,

add_numbers(2, 3)

Below, add_numbers(2, 3) give off that parameters a and b will get values 2 and 3 respectively.

Function Argument with Default Values In Python

In Python Program, we can bargain default values to function arguments.

We can use the = operator to provide default values. Now for example,

def add_numbers( a = 7,  b = 8):
    sum = a + b
    print('Sum:', sum)


# function call with two arguments
add_numbers(2, 3)

#  function call with one argument
add_numbers(a = 2)

# function call with no arguments
add_numbers()

Output :

Sum: 5
Sum: 10
Sum: 15

In the upon example, notice the function definition-

def add_numbers(a = 7, b = 8):

Below, we have take measures default values 7 and 8 for parameters a and b respectively. Below how this program works

1. add_number(2, 3)

Both values are passed by the function call. Away, these values are used instead of the default values.

2. add_number(2)

Only one value is passed by the function call. So that, following to the positional argument 2 is assigned to argument a, and the default value is used for parameter b.

3. add_number()

No value is passed by the function call. Away, default value is used for both parameters a and b.

Keyword Argument In Python

def display_info(first_name, last_name):
    print('First Name:', first_name)
    print('Last Name:', last_name)

display_info(last_name = 'Cartman', first_name = 'Eric')

Output:

First Name: Eric
Last Name: Cartman

Follow the function call,

display_info(last_name = 'Cartman', first_name = 'Eric')

Below, we have assigned names to arguments by the function call.

Away, first_name in the function call is assigned to first_name in the function definition. likewise, last_name in the function call is assigned to last_name in the function definition.

Function With Arbitrary Arguments In Python

# program to find sum of multiple numbers 

def find_sum(*numbers):
    result = 0
    
    for num in numbers:
        result = result + num
    
    print("Sum = ", result)

# function call with 3 arguments
find_sum(1, 2, 3)

# function call with 2 arguments
find_sum(4, 9)

Output:

Sum =  6
Sum =  13

In the upon example, we have maked the function find_sum() that accepts unrestricted arguments. Follow the lines

find_sum(1, 2, 3)

find_sum(4, 9)
Previous
Variables in Python - Practical Examples of Python Variable
Next
Python Datatypes Mastering the Essentials - Complete Guide