Skip to main content

Command Palette

Search for a command to run...

C - Functions

#C Programming

Published
4 min readView as Markdown
C - Functions
I

One code at a time

Just know that Functions in C are simply used to perform a specific task. It is usually a block of code. Functions are handy because you only need to write the code once, and then you can use it over and over; all you need to do is call it. Every C program has at least one function, which is main().

Consider main() as the entry point or front door of a C program. When you start a C program, the computer enters through this 'door' and begins carrying out the program's instructions. It's essentially where every C program kicks off its journey. main() is a built-in function.

Types of Function

In C, there are two main types of functions:

  • Built-in Functions (Standard Library Functions):

    In C, built-in functions are pre-defined components of standard libraries, such as scanf() for input and printf() for output, which you can use immediately, saving you from creating them yourself.

      //main() is a function used to execute code
      //printf() is a function used to output/print text to the screen
      int main() 
      {
          printf("C is not bad");
          return 0;
      }
    
  • User-Defined Functions:

    These are custom-made functions created by programmers to perform specific tasks. These functions are designed by the user and can be used throughout the program, making it easier to organize and reuse code.

      //myFunction will be a customizable function
      void myFunction() 
      {
        // code to be executed
      }
    

    Creating and using a user-defined function in C involves several steps. Let's go through them step by step:

    1. Function Declaration (Prototype):

      You begin by declaring (prototyping) your function at the top of your program. This tells the compiler about the function's name, return type, and any parameters it takes (if any)

       // Function declaration (prototype)
       returnType functionName(parameterType1, parameterType2, ...);
      

      returnType is the data type of the value the function returns. Some functions perform the desired operations without returning a value. In this case, the return_type is the keyword void.

      functionName is the actual name you want to give your function.

      parameter is like a placeholder. When a function is called, you pass a value to the parameter. This value is referred to as actual parameter or argument. The parameter list defines the type, order, and count of these values. Functions can also have no parameters.

      Let's take an example: We want to create a function that gives the maximum of two numbers

       //Function declaration
       int maxNum(int x, int y);
      
    2. Function Definition:

      Next, you define the function. This is where you write the actual code for what the function should do. The function definition typically appears after the main() function. Let's continue with the above example

       //Function definition
       int maxNum(int x, int y)
       {
               int result;
      
               if (x > y)
               {
                       result = x;
               }
               else
               {
                       result = y;
               }
       }
      
    3. Function Call:

      Now, you can call your user-defined function in the main() function or elsewhere in your program. To call the function, simply use its name followed by parentheses. If the function returns a value, you can assign it to a variable or use it directly.

       int main(void)
       {
               int a = 300;
               int b = 250;
               int value;
      
               //Calling the maxNum function with arguments a and b        
               value = maxNum(a, b);
      
               printf("Maximun number is: %d\n", value);
      
               return 0;
       }
      
    4. Compilation and Execution:

      Compile the program, then run the resulting executable. When you run the program, it will execute the code within the main() function, which includes the function call to maxNum. The function maxNum will perform its task and return the result, which is then printed in the main() function.

      Here's the complete code:

       #include <stdio.h>
      
       //Function declaration
       int maxNum(int x, int y);
      
       int main(void)
       {
               int a = 300;
               int b = 250;
               int value;
      
               //Calling the maxNum function with arguments a and b        
               value = maxNum(a, b);
      
               printf("Maximun number is: %d\n", value);
      
               return 0;
       }
      
       //Function definition
       int maxNum(int x, int y)
       {
               int result;
      
               if (x > y)
               {
                       result = x;
               }
               else
               {
                       result = y;
               }
       }
      

      Output:

       Max value is: 300
      

Here is another working example of a function that prints the lowercase letters of the alphabet:

    #include <stdio.h>

    // Function prototype (declaration)
    void printAlphabet();

    int main() 
    {
        printf("Alphabet in lowercase:\n");

        // Function calling
        printAlphabet();

        return 0;
    }

    // Function definition
    void printAlphabet() 
    {
        char letter;
        for (letter = 'a'; letter <= 'z'; letter++) 
        {
            printf("%c ", letter);
        }
        printf("\n"); // New line after printing the alphabet
    }
  1. We have a main() function that simply displays a message and then calls the printAlphabet() function.

  2. The printAlphabet() function uses a for loop to iterate through lowercase letters from 'a' to 'z' and print each letter followed by a space.

  3. After printing all the letters, it adds a new line (\n) to move to the next line.

    Output:

     Alphabet in lowercase:
     a b c d e f g h i j k l m n o p q r s t u v w x y z
    

    Keep breathing!

    Stay curious!!

    Keep coding!!!

The world of C programming

Part 2 of 2

C programming made easy!!! As one delves deeper into the world of C, one realizes it's fun, though it can get a bit tricky here and there. I'm here to make it easier for you.

Start from the beginning

C - Pointers

#C Programming