Functions & Scope

ECON 3209 · Week 2, Lecture 2 · Kerala Agricultural University

Department of Development Economics, KAU

Autumn 2026

Learning Outcomes

By the end of this lecture, you will be able to:

  1. Define functions using def and call them with appropriate arguments
  2. Use parameters, default values, and keyword arguments
  3. Return one or more values from a function
  4. Apply *args and **kwargs in flexible function design
  5. Distinguish between local and global scope in Python

Why Economists Use Functions

  • Functions let us reuse the same logic many times
  • They reduce copy-paste errors in data analysis
  • They make notebooks easier to read and test
  • In econometrics, functions are useful for repeated formulas and indicators
  • Good functions turn messy code into clear economic procedures

If you compute loan interest for 50 borrowers, a function is safer than rewriting the same formula 50 times.

Basic Function Syntax

  • def starts a function definition
  • The function name should be descriptive
  • Parameters go inside parentheses
  • The indented block is the function body
  • The function runs only when it is called

Example: Farm Revenue Function

  • A function can receive inputs and produce output
  • return sends a value back to the caller
  • Store the returned value in a variable when needed
  • This makes economic formulas reusable and transparent

Parameters and Arguments

  • A parameter is the variable inside the function definition
  • An argument is the actual value passed when calling the function
  • Functions can use positional or keyword arguments
  • Clear parameter names improve readability
  • Default values make functions more convenient

Returning Multiple Values

  • Python can return more than one value at once
  • The returned values are packed in a tuple
  • We often unpack them into separate variables
  • This is useful for reporting both totals and components

Functions Inside Loops

  • Functions and loops work naturally together
  • The loop handles repetition
  • The function handles the decision rule
  • This is more readable than repeating the whole if block each time

Flexible Inputs with *args

  • *args collects extra positional arguments into a tuple
  • Use it when the number of inputs may vary
  • It is helpful for totals, averages, and custom summaries
  • Choose a meaningful function name so flexibility does not reduce clarity

Named Options with **kwargs

  • **kwargs collects extra keyword arguments into a dictionary
  • It is useful for optional descriptors or settings
  • But do not overuse it when fixed parameters would be clearer
  • Good design balances flexibility with simplicity

Scope: Local vs Global Variables

  • A variable created inside a function is local to that function
  • A variable created outside functions is often called global
  • Local variables disappear after the function finishes
  • Functions should usually rely on inputs rather than hidden globals
  • This makes code easier to debug and reuse

Best Practices for Writing Functions

  • Keep each function focused on one task
  • Use descriptive names such as compute_interest() instead of do_it()
  • Add docstrings to explain purpose, inputs, and outputs
  • Test functions on small examples before using large datasets
  • Prefer returning values instead of printing everything inside the function

Exercise

Write a function called net_income that takes revenue and cost as inputs and returns profit.

Then call the function for: - Revenue = 125000, Cost = 86000 - Revenue = 98000, Cost = 102000

Finally, print whether each farm made a profit or a loss.

Summary

  • ✅ Functions package reusable logic using def
  • ✅ Parameters and arguments let us customize function behaviour
  • return sends results back for later use
  • *args and **kwargs allow flexible inputs when needed
  • ✅ Local scope keeps variables contained inside a function
  • ✅ Well-designed functions make econometrics code modular and reliable

Next Lecture

List Comprehensions & Lambda

  • We will learn compact ways to transform data
  • You will see how comprehensions and anonymous functions speed up data preparation
  • These tools are especially useful before moving into NumPy and pandas