Functions in SAPL

Learn how to create reusable blocks of code with functions

Learning Objectives

  • Understand what functions are and why they're important in programming
  • Learn how to define and call functions in SAPL
  • Master function parameters and return values
  • Explore variable scope within functions
  • Apply functions to solve practical programming problems

Introduction to Functions

Functions are reusable blocks of code designed to perform a specific task. They allow you to organize your code into manageable, modular pieces, making it more readable, maintainable, and reusable.

Think of functions as mini-programs within your program. You can define a function once and then call it multiple times from different parts of your code, avoiding the need to repeat the same code over and over.

In SAPL, functions are defined using the ስራ keyword, which means "work" or "function". In this tutorial, we'll explore how to define and use functions, work with parameters and return values, and apply functions to solve practical problems.

Defining Functions

To define a function in SAPL, you use the ስራ keyword followed by the function name, parentheses (which may contain parameters), and a colon. The function body is indented and contains the code that will be executed when the function is called.

Basic Function Definition

Here's the basic syntax for defining a function in SAPL:

ስራ የተግባር_ስም():
    # የተግባር አካል
    # ተግባሩ ሲጠራ የሚፈጸም ኮድ
SAPL

Let's create a simple function that prints a greeting:

# ተግባር ይግለጹ
ስራ ሰላምታ():
    አሳይ("ሰላም ዓለም!")

# ተግባሩን ይጥሩ
ሰላምታ()
SAPL

Output:

ሰላም ዓለም!

In this example, we defined a function named ሰላምታ that prints "ሰላም ዓለም!" (Hello World!). We then called the function by writing its name followed by parentheses.

Function Parameters

Parameters are values that you can pass to a function. They allow functions to work with different inputs, making them more flexible and reusable.

Defining Functions with Parameters

To define a function with parameters, you include the parameter names inside the parentheses in the function definition:

# መለኪያዎችን የያዘ ተግባር ይግለጹ
ስራ ሰላምታ(ስም):
    አሳይ("ሰላም, " + ስም + "!")

# ተግባሩን በአርギュመንት ይጥሩ
ሰላምታ("አበበ")
SAPL

Output:

ሰላም, አበበ!

In this example, the function ሰላምታ takes a parameter ስም (name) and uses it to create a personalized greeting. When we call the function, we pass the argument "አበበ", which is assigned to the parameter ስም inside the function.

Multiple Parameters

Functions can have multiple parameters, separated by commas:

# ብዙ መለኪያዎችን የያዘ ተግባር ይግለጹ
ስራ ሰላምታ(ስም, እድሜ):
    አሳይ("ሰላም, " + ስም + "! እድሜህ " + str(እድሜ) + " ነው።")

# ተግባሩን በአርጋመንቶች ይጥሩ
ሰላምታ("አበበ", 25)
SAPL

Output:

ሰላም, አበበ! እድሜህ 25 ነው።

When calling a function with multiple parameters, you must provide arguments in the same order as the parameters are defined in the function.

Default Parameter Values

You can specify default values for parameters, which will be used if no argument is provided when the function is called:

# Define a function with default parameter values
ስራ ሰላምታ(ስም = "እንግዳ"):
    አሳይ("ሰላም, " + ስም + "!")

# Call the function without an argument
ሰላምታ()

# Call the function with an argument
ሰላምታ("አበበ")
SAPL

Output:

ሰላም, እንግዳ! ሰላም, አበበ!

In this example, if no argument is provided for the ስም parameter, the default value "እንግዳ" (guest) is used.

Return Values

Functions can return values using the መልስ keyword, which means "return". This allows functions to compute a result and pass it back to the code that called the function.

Returning Values

Here's how to define a function that returns a value:

# ነባሪ የመለኪያ ዋጋዎች ያለው ተግባር ይግለጹ
ስራ ሰላምታ(ስም = "እንግዳ"):
    አሳይ("ሰላም, " + ስም + "!")

# ተግባሩን ያለ አርጉመንት ይጥሩ
ሰላምታ()

# ተግባሩን በአርጉመንት ይጥሩ
ሰላምታ("አበበ")
SAPL

Output:

ድምር: 8

In this example, the function ድምር takes two parameters, adds them together, and returns the result. When we call the function, the returned value is assigned to the variable ውጤት.

Using Return Values in Expressions

Return values can be used directly in expressions:

# ዋጋ የሚመልሱ ተግባራትን ይግለጹ
ስራ ድምር(a, b):
    መልስ a + b

ስራ ብዜት(a, b):
    መልስ a * b

# የመመለሻ ዋጋዎችን በአገላለጾች ውስጥ ይጠቀሙ
ውጤት = ድምር(5, 3) * 2
አሳይ("ውጤት: " , ውጤት)

ውጤት2 = ብዜት(ድምር(2, 3), 4)
አሳይ("ውጤት2: " , ውጤት2)
SAPL

Output:

ውጤት: 16 ውጤት2: 20

In the second example, we call the ድምር function inside the ብዜት function call. The ድምር function returns 5, which is then passed as an argument to the ብዜት function.

Early Return

The መልስ statement immediately exits the function, returning the specified value. This can be used for early returns based on conditions:

# ቀድሞ የሚመለስ ተግባር
ስራ ክፍፍል(a, b):
    # በዜሮ ማካፈልን አረጋግጥ
    b == 0 ከሆነ:
        አሳይ("በዜሮ መካፈል አይቻልም!")
        መልስ 0  # ቀድሞ መመለስ

    # b ዜሮ ካልሆነ፣ ማካፈሉን አከናውን
    መልስ a / b

# ተግባሩን ጥራ
ውጤት1 = ክፍፍል(10, 2)
አሳይ("ውጤት1: " , ውጤት1)

ውጤት2 = ክፍፍል(10, 0)
አሳይ("ውጤት2: " , ውጤት2)
SAPL

Output:

ውጤት1: 5.0 በዜሮ መካፈል አይቻልም! ውጤት2: 0

In this example, if b is zero, the function prints an error message and returns 0 immediately, without executing the division.

Variable Scope in Functions

Variable scope refers to the region of your code where a variable is accessible. In SAPL, variables defined inside a function are local to that function and cannot be accessed from outside.

# አለምአቀፋዊ ተለዋዋጭ
ዓለም_ቁጥር = 100

# በአካባቢያዊ ተለዋዋጮች ያለ ተግባር
ስራ ምሳሌ():
    # አካባቢያዊ ተለዋዋጭ
    ውስጥ_ቁጥር = 50
    አሳይ("ውስጥ ቁጥር: " , ውስጥ_ቁጥር)
    አሳይ("ዓለም ቁጥር: ", ዓለም_ቁጥር)

# ተግባሩን ጥራ
ምሳሌ()

# ውስጥ_ቁጥር እዚህ መድረስ ስለማይችል ይህ ስህተት ያስከትላል
# አሳይ(ውስጥ_ቁጥር)

# ነገር ግን ዓለም_ቁጥር አሁንም መድረስ ይቻላል
አሳይ("ዓለም ቁጥር እንደገና: " ,ዓለም_ቁጥር)
SAPL

Output:

ውስጥ ቁጥር: 50 ዓለም ቁጥር: 100 ዓለም ቁጥር እንደገና: 100

In this example, ዓለም_ቁጥር is a global variable that can be accessed from anywhere in the code, including inside the function. ውስጥ_ቁጥር is a local variable that can only be accessed inside the ምሳሌ function.

Recursive Functions

A recursive function is a function that calls itself. This can be a powerful technique for solving problems that can be broken down into smaller, similar subproblems.

# ፋክቶሪያልን ለማስላት ተደጋጋሚ ተግባር
ስራ ፋክቶሪያል(n):
    # የመሠረት ሁኔታ
    n <= 1 ከሆነ:
        መልስ 1

    # ተደጋጋሚ ሁኔታ
    መልስ n * ፋክቶሪያል(n - 1)

# ተግባሩን ይጥሩ
ውጤት = ፋክቶሪያል(5)
አሳይ("5! = " + ውጤት) 
SAPL

Output:

5! = 120

In this example, the ፋክቶሪያል function calculates the factorial of a number using recursion. The factorial of a number n (denoted as n!) is the product of all positive integers less than or equal to n.

For example, 5! = 5 × 4 × 3 × 2 × 1 = 120.

Practical Examples

Example 1: Temperature Converter

Let's create functions to convert between Celsius and Fahrenheit:

# ሴልሲየስን ወደ ፋራናይት የሚቀይር **ስራ** (ተግባር)
ስራ ሴልሲየስ_ወደ_ፋራናይት(ሴልሲየስ):
    **መልስ** (ሴልሲየስ * 9/5) + 32

# ፋራናይትን ወደ ሴልሲየስ የሚቀይር **ስራ** (ተግባር)
ስራ ፋራናይት_ወደ_ሴልሲየስ(ፋራናይት):
    **መልስ** (ፋራናይት - 32) * 5/9

# **ስራዎችን** (ተግባራትን) **ሞክር** 
ሴልሲየስ = 25
ፋራናይት = ሴልሲየስ_ወደ_ፋራናይት(ሴልሲየስ)
**አሳይ**(ሴልሲየስ, "°C = " ,ፋራናይት,  "°F")

ፋራናይት = 98.6
ሴልሲየስ = ፋራናይት_ወደ_ሴልሲየስ(ፋራናይት)
**አሳይ**(ፋራናይት ,"°F = " ,ሴልሲየስ,  "°C")
SAPL

Output:

25°C = 77.0°F 98.6°F = 37.0°C

Example 2: Prime Number Checker

Let's create a function to check if a number is prime:

# አንድ ቁጥር ፕራይም (Prime) መሆኑን የሚያጣራ **ስራ** (ተግባር)
ስራ ፕራይም_ነው(n):
    # ከ 2 በታች የሆኑ ቁጥሮች ፕራይም አይደሉም
    n < 2 ከሆነ:
        መልስ ሀሰት

    # ከ 2 እስከ ስኩዌር ሩት የ n ባሉ ቁጥሮች መካፈልን አረጋግጥ
    ለ ቀ በ ውስጥ(2, n**(1/2) + 1): # የ ቀ * ቀ > n የሚለውን አመክንዮ በ n**0.5 ተክተናል
        n % ቀ == 0 ከሆነ:
            መልስ ሀሰት  # አካፋይ ተገኝቷል፣ ፕራይም አይደለም

    መልስ እውነት  # ምንም አካፋይ አልተገኘም፣ ፕራይም ነው

# **ስራውን** (ተግባሩን) **ሞክር** 
ለ num በ ውስጥ(1, 21):
    አሳይ(num , " ፕራይም ነው: " , ፕራይም_ነው(num))
SAPL

Output (partial):

1 is prime: ሀሰት 2 is prime: እውነት 3 is prime: እውነት 4 is prime: ሀሰት 5 is prime: እውነት ...

Best Practices

  • Keep functions focused on a single task

    Each function should have a clear, specific purpose. If a function is doing too many things, consider breaking it down into smaller functions.

  • Use descriptive function names

    Choose names that clearly indicate what the function does. For example, use calculate_area instead of calc or a.

  • Document your functions

    Add comments to explain what the function does, what parameters it takes, and what it returns. This makes your code more maintainable.

  • Keep parameter lists manageable

    If a function requires too many parameters, consider restructuring it or using a data structure to group related parameters.

  • Use return values effectively

    Functions should return values rather than modifying global variables or printing results directly, when possible. This makes them more reusable.

Summary

Key Concepts Learned

  • Functions are reusable blocks of code defined using the ስራ keyword
  • Parameters allow functions to accept input values, making them more flexible
  • Functions can return values using the መልስ keyword
  • Variables defined inside a function are local to that function
  • Recursive functions call themselves to solve problems that can be broken down into smaller subproblems
  • Functions help organize code, reduce repetition, and improve maintainability

Functions are a fundamental building block in programming, allowing you to create modular, reusable code. They're essential for organizing your code, reducing repetition, and making your programs more maintainable. By mastering functions in SAPL, you've taken a significant step toward becoming a proficient programmer.