Functions in C Programming with Examples

In this Function in C Programming Tutorials, you will learn about functions in C programming (both user-defined and standard library functions).

You’ll also find out why functions are employed in programming. A function is a code block that accomplishes a certain task.

Assume you need to write a program that will draw a circle and color it.

To fix this problem, you can build two functions:

  • function to make a circle
  • a color function should be created

Our program is simple to comprehend and reuse since it breaks down a difficult problem into smaller bits.

Functions Type

  • Standard library functions
  • User-defined functions

Standard library functions

In C programming, standard library functions are built-in functions. In header files, these functions are declared.

As an example, The printf() function is a standard library function that displays formatted output (display output on the screen).

The stdio.h header file defines this function.

  • To utilize the printf() function, we must use #include stdio.h> to include the stdio.h header file.
  • The sqrt() function calculates a number’s square root. In the math.h header file, the function is defined.

To learn more, go to C programming’s standard library functions.

C User-defined functions

With the help of an example, you will learn how to build user-defined functions in C programming in this article.

A function is a code block that accomplishes a certain task.

You can define functions in C according to your needs. User-defined functions are what they’re called.

Consider the following scenario:

Assume you need to draw a circle and color it in different ways depending on the radius and hue.

To fix this problem, you can build two functions:

  1. createCircle() is a function that creates a circle.
  2. color() is a function that allows you to change the color

You can also develop your own functions based on your requirements. User-defined functions are functions that are created by the user.

What is the procedure for using a user-defined function?

#include <stdio.h>
void functionName()
{
    ***********
    ***********
}

int main()
{
    ***********
    ***********

    functionName();
    
    ***********
    ***********
}

The main() function is where a C program starts to run. When the compiler sees functionName();, the program’s control is passed to:

 void functionName()

The compiler then begins running the code contained within functionName ().

Once the code inside the function declaration is executed, the program’s control returns to the main() function.

It’s important to remember that function names are identifiers and should be unique.

This is merely a summary of user-defined functions. For further information, go visit the following pages:

  • User-defined Function in C programming
  • Types of User-defined Functions

The benefits of a user-defined function

  • It will be simpler to comprehend, maintain, and troubleshoot the application.
  • Codes that can be reused in different applications
  • A big program can be broken down into smaller chunks. As a result, a huge project might be split among several programmers.

User-defined function example:

Here’s an example of how to multiply two integers. To accomplish this, we’ve constructed a user-defined addNumbers function ().

#include <stdio.h>
int addNumbers(int x, int y);         // function prototype

int main()
{
    int num1,num2,sum;

    printf("Enters two numbers: ");
    scanf("%d %d",&num1,&num2);

    sum = addNumbers(num1, num2);        // function call
    printf("sum = %d",sum);

    return 0;
}

int addNumbers(int x, int y)         // function definition   
{
    int result;
    result = x+y;
    return result;                  // return statement
}

Output of the above example:

Enters two numbers: 9 5
sum = 14

You can test the above example here! ➡ C Online Compiler

Function prototype

A function prototype is merely a function declaration that provides the name, parameters, and return type of the function. It is missing the function body.

A function prototype tells the compiler that the function will be utilized later in the program.

Function prototype syntax

returnType functionName(type1 argument1, type2 argument2, ...);

The function prototype int addNumbers(int a, int b); in the preceding example provides the compiler with the following information:

  • addNumbers is the name of the function ()
  • The function’s return type is int.
  • The function is called with two int arguments.

If the user-defined function is defined before the main() function, the function prototype is not required.

Calling a function

By calling the user-defined function, control of the program is given to it.

Function call syntax

functionName(argument1, argument2, ...);

The function is called in the previous example using the main() function’s addNumbers(n1, n2); line.

Function definition

The definition of a function is a chunk of code that performs a specified activity. In this case, we’ll add two numbers and return the result.

Function description syntax

returnType functionName(type1 argument1, type2 argument2, ...)
{
    //body of the function
}

The control of the program is transferred to the function definition when a function is invoked.

The compiler then begins executing the code contained within a function’s body. Invoking a function with parameters.

The variable provided to the function is referred to as an argument in programming. Two variables, n1 and n2, are provided to the function call in the example above.

The provided arguments in the function declaration are accepted by the parameters a and b. These arguments are known as the function’s formal parameters.

Otherwise, the compiler will throw an error if the type of arguments given to a function and the formal parameters do not match.

If n1 is a char type, a should be a char type as well. If n2 is a float, variable b should be a float as well.

It is also possible to invoke a function without providing an argument.

Return Statement

The return statement marks the end of a function’s execution and returns a value to the caller function.

After the return statement, program control is passed to the calling function.

The value of the result variable is returned to the main function in the example above. This value is assigned to the sum variable in the main() function.

Return Statement Syntax

return (expression);     

For example,

return a;
return (a+b);

The return type stated in the function prototype and function definition must match the type of value returned by the function.

To understand more about passing arguments and returning a value from a function, go to this page.

C Functions Types

In C programming, there are several different types of user-defined functions.

In this course, you’ll learn about various techniques of utilize functions to solve the same problem.

These four applications determine whether or not the integer input by the user is a prime number.

All of the programs below provide the same result, and in each case, we’ve generated a user-defined function. In each case, though, we have taken a different method.

No arguments passed and no return value example:

#include <stdio.h>

void checkPrimeNumber();

int main()
{
    checkPrimeNumber();    // argument is not passed
    return 0;
}

// return type is void meaning doesn't return any value
void checkPrimeNumber()
{
    int n, i, flag = 0;

    printf("Enter a positive integer: ");
    scanf("%d",&n);

    for(i=2; i <= n/2; ++i)
    {
        if(n%i == 0)
        {
            flag = 1;
        }
    }
    if (flag == 1)
        printf("%d is not a prime number.", n);
    else
        printf("%d is a prime number.", n);
}

Output of the above example:

Enter a positive integer:7
7 is a prime number.

Explanation of the above code:

The checkPrimeNumber() function accepts user input, determines whether it is a prime number, and shows the result on the screen.

The empty parenthesis in the main() method’s checkPrimeNumber(); statement indicates that no argument is supplied to the function.

The function’s return type is void. As a result, the function returns no value.

You can test the above example here! ➡ C Online Compiler

Conclusion

A function is a collection of statements that work together to complete a task.

Every C program contains at least one function, main(), and even the simplest programs can specify more functions.

You can partition your code into different functions. It’s up to you how you divide your code into separate functions, but logically, each function should fulfill a specific duty.

The name, return type, and parameters of a function are all specified in a function declaration. The body of the function is defined by a function definition.

The C standard library includes a number of built-in functions that you can use in your program.

Strcat(), for example, concatenates two strings, memcpy() copies one memory region to another, and many other procedures.

Inquiries

If you have any questions or suggestions about Functions in C Programming with Examples – Recursive and Inline, please feel free to leave a comment below.


Leave a Comment