General and Web Programming Fundamentals
  • Introduction
  • Program creation and design
    • Program design
      • Algorithms
      • Pseudocode
    • Programming conventions
    • Writing programs
      • Source code editors
      • Integrated Development Environments
      • Code repositories/Version control
      • Compilers/Interpreters
  • Programming Fundamentals
    • Operators
      • Arithmetic
      • Logical
      • Assignment
    • Constants and Variables
    • Datatypes
      • Primitive Datatypes
        • Character
        • Integer
        • Boolean
        • Floating point
        • Nothing (Null)
      • Composite Datatypes
        • Arrays
        • Strings
        • Classes
        • Structs
      • Literals
    • Data structures
      • Lists
      • Queues
      • Stacks
      • Map/dictionary
      • Trees
      • Graphs
    • Control structures
      • Selection (Conditional)
        • If/Else
        • Ternary
        • Switch
      • Iteration (Loops)
        • For loops
        • While loops
        • Do-While loops
        • For-Each loops
    • Functions
      • Parameters and arguments
      • Lambda expressions
      • Higher Order Functions
    • Space and Time
    • Scope
    • Standard libraries
  • Programming Paradigms
    • Procedural (Imperative) Programming
    • Object-oriented programming
    • Functional Programming
    • Declarative Programming
    • Event Driven programming
  • Programming Languages
    • Short history of programming
    • Low-level programming languages
    • High-level programming languages
  • Web Development
    • What is the web?
      • Web browsers (clients)
      • Webservers (serving web pages)
      • W3C
    • Markup languages
      • HTML
        • HTML Tags
      • Cascading Style Sheets (CSS)
        • CSS Properties
      • XML
      • Markdown
    • Scripting Languages
      • JavaScript
      • TypeScript
    • JSON
    • JavaScript Frameworks
  • Acknowledgements
    • About the author(s)
  • License
Powered by GitBook
On this page

Was this helpful?

Export as PDF
  1. Programming Fundamentals

Functions

Functions/Methods in Computer Programming

In computer programming, functions or methods (the terms are often used interchangeably, though with some differences depending on the programming paradigm and language) are reusable blocks of code designed to perform a specific task. A function typically takes input (known as arguments or parameters), performs some operations on the input, and then returns an output. The main purpose of functions is to segment large programs into smaller, manageable, and reusable components. This enhances readability, makes maintenance easier, and helps avoid redundancy.

Functions in Python

A Python function is defined using the def keyword, followed by the function name and parentheses (()) which may include arguments. The code block within the function starts with a colon (:) and is indented.

Example:

# Define a function in Python
def greet(name):
    return f"Hello, {name}!"

# Call the function
print(greet("Alice"))

Output:

Hello, Alice!

Methods in C#

In C#, a method is a code block containing a series of statements. Methods are defined within classes or structs, and they can perform operations and return values. Methods in C# are defined using the return type, the name of the method, and a pair of parentheses which can include parameters.

Example:

using System;

class Program
{
    // Define a method in C#
    static string Greet(string name)
    {
        return $"Hello, {name}!";
    }

    static void Main(string[] args)
    {
        // Call the method
        Console.WriteLine(Greet("Bob"));
    }
}

Output:

Hello, Bob!
PreviousFor-Each loopsNextParameters and arguments

Last updated 1 year ago

Was this helpful?