Skip to content

Latest commit

 

History

History
67 lines (46 loc) · 1.05 KB

File metadata and controls

67 lines (46 loc) · 1.05 KB

Functions — Writing Reusable and Modular Code

Build once, use everywhere — scan, parse, connect, log. Functions are the backbone of every security tool.

Defining a Function

def greet(name):
    print(f"hello, {name}")

greet("alice")

Return Values

def add(a, b):
    return a + b

result = add(3, 5)   # 8

Default Arguments

def scan(host, port=80):
    print(f"scanning {host}:{port}")

scan("192.168.1.1")          # port 80
scan("192.168.1.1", 443)     # port 443

Keyword Arguments

def connect(host, port, protocol):
    print(f"{protocol}://{host}:{port}")

connect(host="10.0.0.1", port=22, protocol="ssh")

Variable Number of Arguments

def total(*args):
    return sum(args)

def print_info(**kwargs):
    for k, v in kwargs.items():
        print(f"{k}: {v}")

total(1, 2, 3, 4)        # 10
print_info(name="alice", age=25)

Scope

x = 10  # global

def my_func():
    x = 5  # local — different variable
    print(x)  # 5

my_func()
print(x)  # 10