Functions

In programming a function (also known as a subroutine) is a sequence of instructions that performs a specific task. To put that in simpler English: Functions are a block of code that does a specific task and can be called from elsewhere in your program whenever you need to execute said task. Functions can allow you to:

  • View your program as a bunch of sub-steps.
  • Reuse code instead of rewriting logic.
  • Keep variable namespace clean. Variables defined in a function only exist during the duration of the function executing.
  • Test small parts of our program in isolation from the rest of our program.

How to write a function

So like most things in programming any programming language can handle how functions are defined differently. Some good general guidelines are that programming languages will either have you define what data type your function will return after it finishes executing or the programming language allows you to define functions dynamically and will handle the information returned from the funciton for you. Most programming languages will also have you define if any data types get passed into your function for the function to use. Let's see some exmaples of creating functions, we will first go over some languages that require you to define the data type the function will return:

  • C:

    void greeting (string name) {
      printf("Hello %s!", name);
    }
    
  • Java:

    public class Main {
      static void greeting(String name) {
        System.out.println("Hello " + name + "!");
      }
    }
    

Now let's looks at some examples of programming languages that let you dynamically create functions:

  • Bash:

    greeting() {
      echo "Hello $1!";
    }
    
  • Python:

    def greeting(name) {
      print("Hello " + name + "!")
    }
    

How to call a function

To call a function and execute it in most programming languages you just call the name of the function and pass it any data types it is expecting if it is expecting any input variables. It is important to note that in some programming languages a function has to be defined before it can be called so the function should be placed above the line where you are calling it in the file. Some programming languages don't require you to define the input data for the function and will allow you to dynamically pass in arguments. Let's look at how to call the functions we defined above:

  • C:

    void greeting (string name) {
      printf("Hello %s!", name);
    }
    
    int main() {
      greeting("Maker");
    }
    
  • Java:

    public class Main {
      static void greeting(String name) {
        System.out.println("Hello " + name + "!");
      }
    
      public static void main(String[] args) {
        greeting("Maker");
      }
    }
    
  • Python:

    def greeting(name) {
      print("Hello " + name + "!")
    }
    
    greeting("Maker");
    

Bash is one of those languages that doesn't require you to define the input arguments for your function. Let's see how to call the bash function we defined above:

  • Bash:

    greeting() {
      echo "Hello $1!";
    }
    
    greeting("Maker");
    
This page was last updated: 2023-04-12 Wed 20:23. Source