C Function

What is Function, and how to declare it?

* Functions are very useful when a block of statements has to be written/executed again and again also, functions are useful when the program size is too large or complex.

What is Function?

In C, a function is a self-contained block of code that performs a specific task or set of tasks. They cannot be run independently and are always called by the 'main()' program or by other functions.

Generally, there are two types of functions available in C, One is a built-in function, and another is called a user-defined function. Let's deep dive into both types of functions:

Built-in Functions:

In the C programming language, there are several built-in functions provided by the C Standard Library. These functions are included in the standard C library and are available for use in your C programs without requiring any special headers or additional setup. Here are some commonly used built-in functions in C:

print() scanf() strlen() strcpy() strcat()
  • print()
  • scanf()
  • strlen()
  • strcpy()
  • strcat()

* We already learned their working process in our previous lessons.

User-defined Function:

A user-defined function is a function that you create and define yourself, rather than using one of the built-in functions provided by the C standard library. User-defined functions allow you to encapsulate a specific piece of code into a reusable block, making your code more organized and easier to maintain.

Here's the basic syntax for defining a user-defined function in C:

return_type function_name(parameters) {
    // Function body (code)
    // You can declare and use variables here.
    // Perform operations, calculations, etc.
    // Return a value of the specified return_type

Let's break down the components of a user-defined function:

return_type: This is the data type of the value that the function will return. It can be 'int', 'float', 'char', 'void', or any other valid C data type. If the function doesn't return a value, you should use 'void' as the return type.

function_name: This is the name you give to your function. It should be a valid identifier and follow C naming conventions. Function names are case-sensitive in C.

parameters: These are the input values that the function accepts. You specify the data type and name of each parameter (arguments), separated by commas. If the function doesn't take any parameters, you can leave this section empty or use 'void'.

Function body (code): This is the block of code enclosed within curly braces {}. It contains the instructions that the function will execute when called.

return: If the function has a return type other than 'void', you should use the 'return' statement to specify the value that the function will return. For example, return result;.

Here's an example of a simple user-defined function that adds two integers and returns the result:

c Copy Code
#include <stdio.h>

int add(int num1, int num2) {
    int sum = num1 + num2;
    return sum;

int main() {
    int a = 5, b = 3;
    int result = add(a, b);
    printf("The sum of %d and %d is %d\n", a, b, result);
    return 0;
The sum of 5 and 3 is 8

In this example, the 'add' function takes two integer parameters ('num1' and 'num2'), calculates their sum, and returns the result. The 'main' function calls 'add' and prints the result.

Note: User-defined functions can be called multiple times from different parts of your program, making your code more modular and easier to understand.

What's Next?

We've now entered the finance section on this platform, where you can enhance your financial literacy.