English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

C Language Basic Tutorial

C Language Flow Control

C Language Functions

C Language Arrays

C Language Pointers

C Language Strings

C Language Structures

C Language Files

C Others

C Language Reference Manual

C User-Defined Functions

In this tutorial, you will learn how to create user-defined functions in C programming language with the help of examples.

Functions are code blocks that perform specific tasks.

C allows you to define functions as needed. These functions are called user-defined functions. For example:

Suppose you need to create a circle and color it based on its radius and color. You can create two functions to solve this problem:

  • createCircle() function

  • color() function

Example: User-defined function

This is an example of adding two integers. To perform this task, we create a user-defined addNumbers().

#include <stdio.h>
int addNumbers(int a, int b);         //Function prototype
int main()

    int n1,n2,sum;
    printf("Enter two numbers: ");
    scanf("%d %d", &n)1&n2);
    sum = addNumbers(n1, n2);        // Function call
    printf("sum = %d", sum);
    return 0;

int addNumbers(int a, int b)         //Function definition

    int result;
    result = a+b;
    return result;                  //Return statement

Function prototype

The function prototype is just a declaration of the function, used to specify the name, parameters, and return type of the function. It does not contain the function body.

The function prototype provides information to the compiler that the function can be used later in the program.

The syntax of function prototype

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

In the above example, the function prototype int addNumbers(int a, int b); provides the following information to the compiler:

  1. The name of the function is addNumbers()

  2. The return type of the function is int

  3. Two parameters of type int are passed to the function

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

Call a function

Program control is transferred to the user-defined function through a call.

The syntax of function call

functionName(argument1, argument2, ...);

In the above example, the function addNumbers(n1, n2); call.

Function definition

The function definition contains a code block that executes a specific task. In our example, it adds two numbers and returns.

The syntax of function definition

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

    //Function body

When a function is called, program control is transferred to the function definition, and the compiler begins to execute the code within the function.

Passing parameters to a function

In programming, a parameter refers to a variable passed to a function. In the above example, two variables n1and n2

The parameters a and b accept the parameters passed in the function definition. These parameters are called formal parameters of the function.

The parameter type and the form parameter passed to the function must match; otherwise, the compiler will generate an error.

If n1If it is a char type, then a should also be a char type. If n2If it is a floating-point type, then the variable b should also be a floating-point type.

Functions can also be called without passing any parameters.

return statement

The return statement terminates the execution of the function and returns a value to the calling function. After the return statement, program control is transferred to the calling function.

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

The syntax of the return statement

return (expression);

For example,

return a;
return (a+b);

The type of the value returned from the function must match the return type specified in the function prototype and function definition.

Access this page to learn more aboutPassing Parameters and Returning Values from FunctionsMore Information.