English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
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
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
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.
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:
The name of the function is addNumbers()
The return type of the function is int
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.
Program control is transferred to the user-defined function through a call.
functionName(argument1, argument2, ...);
In the above example, the function addNumbers(n1, n2); call.
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.
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.
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.
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.