What is a C function

In this lesson, we discuss what a C function is, but before checking a function in C language, it is better to understand the concept. If we consider a car, it consists of many components, each of which performs a specific task. For instance, the engine produces mechanical power to move the car. Therefore, the engine receives fuel, oil, and electricity and then produces mechanical power. In fact, it receives the input and delivers the appropriate output. Now, let’s move on to the functions.

C function advantages

Functions are like the engine, so in C language, they receive input and deliver appropriate output. Functions are code blocks, and using them in programming has advantages, some of which are as follows:

  1. Breaking the program into smaller parts to make it easier to solve problems
  2. Designing, testing, and debugging application components independently
  3. Reusing the program independently
  4. Doing the project as a team
  5. Increasing the readability of the program and making it easier to understand
  6. Simplifying the program debugging
  7. Reducing the size of the program

C Function components

A function consists of 4 parts: input, output, function name, and function body, which are explained below:

Function input is the value(s) that the function receives in the program.
Function output
is the value it returns to the program.
The function name
is used to call the function in the program and follows the naming rules of C language.
The function body Contains the statements that the function is supposed to execute.

Note 1: The input and output of a function in C programming are variables, arrays, strings, etc.

Note 2: By default, functions can take multiple inputs and deliver only one output. But there is a solution for having multiple outputs, which we will discuss in the function and pointer lesson.

Note 3: Functions with any name introduced (uppercase or lowercase letters) with the same name must be used in the program.

Function definition

If you have reviewed the previous lessons, you have noticed that every part of the C language needs to be defined. So, before anything, the function must be defined. After defining the function, we call it wherever it is needed in the program.
But where do you think the function can be defined? You may wonder if the function definition location is important.
Yes, it is very important. Basically, in C language, each member must be defined in a specific place. Otherwise, we will get an error. The definition location of all C language components depends on the main function.

The main function is a function that is present in all programs that are written, and other functions and the main body of the program are executed inside this function. Without the main() function, your program cannot be executed.

In C language, functions can be defined in 3 areas:

  1. Before the main function
  2. After the main function
  3. In the library files (we will explain the library later on during this course)

Functions can be defined before the main function; Therefore, by starting the program, the functions are seen by the program and can be used. Below you can see the general format of the function definition:

<output type> <function name>(parameter1, parameter2, ...)
{
    //function body
}

Now, pay attention to the following example:

#include <stdio.h>

//function definition
void myFunc(void){
    //myfunc body
}

int main(){
    //main body
}

Function myFunc() is defined before the main() function.

But if the function is defined after the main function, the program must be informed by the declaration before the main function. That is, there are functions after the main function because when a program starts, only the code before and inside The main function is visible to the program. Therefore, creating a function using the second method includes two steps:

  1. function definition
  2. function declaration(prototype)

Look at the function declaration before the main function:

<output type> <function name>(parameter1, parameter2, ...);

The difference between the function definition and function declaration is in the function body. There is no need to write the function body for the declaration, and only writing the output, name, and parameters of the function is enough. For a better understanding, pay attention to the following example:

#include <stdio.h>

//function declaration
void myFunc(vodi);

int main(){
    //main body
}

//function definition
void myFunc(void){
    //myfunc body
}

Function categories

So far, we have learned what the function is, what its components are, how it should be defined, and what the function template is used for. But the function in C language can be divided into 4 categories from the input and output point of view:

  1. Function with input, with output
  2. Function with input, without output
  3. Function without input, with output
  4. Function without input, without output

Function with input, with output

This type of function can be likened to a machine that receives raw materials from the input, performs the operations on it and then returns the final product. Consider the following example:

int fx (unsigned char x, unsigned char y){
    int s;  
    s = x * y;  
    return s;  
}
  1. The fx function takes two values ​​as inputs and returns the product of the two values ​​to the function’s output.
  2. The function’s output data type is int, and inputs are unsigned char.
  3. Inside the function, a local variable of type int named s is introduced, which holds the result of multiplying two inputs x and y.
  4. In the function’s last line, the variable s is returned to the output.
  5. This function can be defined above the main function or in the header files, or below the main function.

You can see the declaration of this function below:

int fx (unsigned  char x, unsigned char y);

The following example shows how to use the function defined above, in the program:

#include <stdio.h>

int fx (unsigned char x, unsigned char y);

int w,z;
unsigned char i=4, j=6;

void main(void){
    w = fx(4, 5);  
    z = fx(i, j);  
    printf("%d, %d", w, z);
}

int fx (unsigned char x, unsigned char y){
    int s;  
    s = x * y;  
    return s;  
}

As an exercise, check what the result ​​of printf() would be after executing this code.

Function with input, without output

A function with an input without an output is a function that receives numeric values ​​from the input but does not return a value to the program. This type of function can print the values ​​received from the input on the screen. Because this function has no output, the word void is used in its output. Consider the following example:

void gx (unsigned char x, unsigned char y)
{
    unsigned char s;  
    s = x & y;  
    printf("x & y is %s\n", s);  
}
  1. A function called gx receives two values ​​from the input and prints their bitwise AND on the screen.
  2. void is placed in the output of the function.
  3. A function with no output does not have a return statement because no value is returned to the output.

Function without input, with output

A function with no input, with an output, is a function that does not receive values ​​from the input but returns a value to the program. For example, this type of function can read some variables and return them to the output. In this type of function, the word void is placed in the function’s input. Consider the following example:

usigned char kx(void)
{
    int a, b, c;
    printf("Enter value1: ");
    scanf("%d", &a);
    printf("Enter value2: ");
    scanf("%d", &b);
    c = a + b;  
    return c;  
}
  1. The function kx receives two integers from the input using scanf(). (we discuss this function in the next lesson)
  2. The output is the summation of a and b.

Function without input, without output

A no-input no-output function is a function that neither receives values ​​from the input nor returns values ​​to the output. Usually, functions that only perform operations on variables are like this. Consider the following example:

void SayHello(void)
{
    printf("hello world" 
}

The appropriate length for function output

The type of data that the functions return to the output should be chosen in such a way that they hold the result of the calculation with the largest value. To understand the issue, pay attention to the following example:

Consider a function that receives two values ​​of unsigned char type in the input and their multiplication is returned to the output of the function. The unsigned char data type is an 8-bit unsigned type and can hold numeric values ​​from 0 to 255. If these two inputs have their maximum value (255), their multiplication would be 16 bits; Therefore, the output of this function must be defined as unsigned int so that an error does not occur during execution

Was this helpful?
[1]
Scroll to Top
Scroll to Top