Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Python Functions

Tech May 27 4

Table of Contants- Function Definition

  • Function Concepts
  • Three Forms of Function Definision
  • Return Values in Functions
  • Function Parameters

Function Definition

def register_user():
    # Registration functionality
    print('Registration function')
    user_input = input('Enter your username: ')
    password_input = input('Enter your password: ')
    with open('user_info.txt','a',encoding='utf8') as file:
        if ':' in user_input or ':' in password_input:
            print('Invalid input, cannot contain ":"')
        else:
            file.write(f'{user_input}:{password_input}')
            print('Saved successfully')


# Login functionality
# print('Login function')
# with open('user_info.txt','r',encoding='utf8') as file:
#     data = file.read()
#     user_data_list = data.split(':')
# username,password = user_data_list[0],user_data_list[1]
# username_input = input('Enter your username: ')
# password_input = input('Enter your password: ')
#
# if username == username_input and password == password_input:
#     print('Login successful')
# else:
#     print('Login failed')

# Above only defines a function, creating a tool (syntax is checked during definition phase but code isn't executed).
# To execute the function, call it directly.

register_user()


Function Concepts

# Function: In mathematics, a function maps inputs to outputs - this concept has no relation to mathematical functions.

# Sewer system --> Clearing sewers --> Get a tool (buy one) --> Convenient, ready to use
# A function is like this tool --> performs a specific task
# Functions separate tools, not reducing code volume
# Guess the number game code
def higher_number():
    num1 = 10
    num2 = 20
    if num1 > num2:
        print(num1)
    else:
        print(num2)
def calculate_sum():
    num1 = 10
    num2 = 20
    print(num1+num2)
# Use the functions
higher_number()
calculate_sum()

# Function syntax
'''
# Create a tool, not yet used
def function_name(tool_name): --> follows variable naming rules
    <function implementation>
# Call function when needed
function_name()
'''
# Functions separate functionalities and allow calling when required


Three Forms of Function Definition

# Empty function
def register_user():
    # TODO: Registration functionality, temporarily not implemented
    pass    # Placeholder for empty function

# Parameterized function (function with parameters)
def compare_numbers(a,b):
    if a > b:
        print(a)
    else:
        print(b)
compare_numbers(30,40)  # Must provide arguments when calling
# When using a flashlight, you don't modify internal components, just activate it (pass an argument)

# Parameterless function
def simple_function():
    print(1)
simple_function()


Return Values in Funnctions

# def add_numbers(a,b):
    # Various logic
#     return a + b    # Function return value
# result = add_numbers(10,20)
# print(result)


# def add_numbers(a,b):
#     # Various logic
#     print(a+b)
#     return a + b    # Function return value; terminates function execution
#     print(1)
# print(2)
# result = add_numbers(10,20)
# print(result)

# def add_numbers(a,b):
#     # Various logic
#     print(a+b)
#     # return a + b    # Function return value; terminates function execution
#     print(1)
# print(2)
# result = add_numbers(10,20)    # If no return, assigning function call result to variable defaults to None
# print(result)

def add_numbers(a,b):
    # return(a,b,a+b) # Return can return any data type
    return a,b,a+b  # Return can return any data type, multiple values without parentheses default to tuple
result = add_numbers(1,2)
print(result)
a,b,c = add_numbers(1,2)    # Unpacking
print(a,b,c)


Function Parameters

# def add_numbers(a,b):
    # Various logic
#     return a + b    # Function return value
# result = add_numbers(10,20)
# print(result)


# def add_numbers(a,b):
#     # Various logic
#     print(a+b)
#     return a + b    # Function return value; terminates function execution
#     print(1)
# print(2)
# result = add_numbers(10,20)
# print(result)

# def add_numbers(a,b):
#     # Various logic
#     print(a+b)
#     # return a + b    # Function return value; terminates function execution
#     print(1)
# print(2)
# result = add_numbers(10,20)    # If no return, assigning function call result to variable defaults to None
# print(result)

def add_numbers(a,b):
    # return(a,b,a+b) # Return can return any data type
    return a,b,a+b  # Return can return any data type, multiple values without parentheses default to tuple
result = add_numbers(1,2)
print(result)
a,b,c = add_numbers(1,2)    # Unpacking
print(a,b,c)


Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.