English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
In this tutorial, I will introduce functions in C language programming (user-defined functions and standard library functions). In addition, you will also learn why functions are used in programming.
A function is a code block that performs a specific task.
Suppose you need to create a program to create a circle and color it. You can create two functions to solve this problem:
Function to create a circle
Function to create color
Breaking complex problems into smaller pieces makes our programs easier to understand and reuse.
C functions have the following advantages.
By using functions, we can avoid rewriting the same logic over and over again in the program./Code.
We can call C functions multiple times in the program and at any location in the program.
When a large C program is divided into multiple functions, we can easily track it.
Reusability is the main achievement of C functions.
However, function calls are always a cost in C programs.
C functions include three aspects.
Function declaration Must be global in the C programDeclarationFunction to inform the compiler of the function name, function parameters, and return type.
Function call Can be from any location in the programCallFunction. The parameter list must be the same in both the function call and the function declaration. We must pass the same number of functions as declared in the function declaration.
Function definition It contains the actual statements to be executed. This is the most important aspect involved when calling a function. Here, we must note that the function can only return one value.
Serial number | C function aspect | Syntax |
---|---|---|
1 | Function declaration | return_type function_name(argument_list); |
2 | Function call | function_name(argument_list) |
3 | Function definition | return_type function_name(argument_list){function body;} |
The syntax for creating a function in C language is as follows:
return_type function_name(data_type parameter...){ //Code to be executed }
There are two types of functions in C programming:
Standard library functions:Functions declared in C header files, such as scanf(), printf(), gets(), puts(), ceil(), floor(), etc.
User-defined functions:User-defined functions in C, we can use them multiple times. It reduces the complexity of large programs and optimizes the code.
C functions may or may not return values from the function. If you do not need to return any value from the function, use void as the return type.
Let's look at a simple C function example that does not return any value from the function.
An example without a return value
void hello(){ printf("hello "); }
If you need to return any value from a function, you need to use any data type, such as int, long, char, etc. The return type depends on the value to be returned from the function.
Let's look at a simple C function example that returns an int value from the function.
Return value example:
int get(){ return 10; }
In the above example, we must return10as a value, so the return type is int. If you need to return a floating-point value (for example10.2,3.1,54.5etc.), you need to use float as the return type of the method.
float get(){ return 10.2; }
Now, you need to call this function to get the value of this function.
Functions can accept or not accept any parameters. They may or may not return any value. Based on these facts, there are four different aspects of function calls.
A function with no parameters and no return value
A function with no parameters but a return value
A function with parameters but no return value
A function with parameters and return values
Example1
#include<stdio.h> void printName(); void main () { printf("Hello "); printName(); } void printName(); { printf("www.oldtoolbag.com"); }
Output the result
Hello www.oldtoolbag.com
Example2
#include<stdio.h> void sum(); void main() { printf("\nCalculate the sum of two numbers:"); sum(); } void sum() { int a, b; printf("\nPlease enter two numbers"); scanf("%d %d", &a, &b); printf("Sum is %d", a+b); }
Output the result
Calculate the sum of two numbers: Please enter two numbers 10 24 Sum is 34
Example1
#include<stdio.h> int sum(); void main() { int result; printf("\nCalculate the sum of two numbers:"); result = sum(); printf("%d", result); } int sum() { int a, b; printf("\nPlease enter two numbers"); scanf("%d %d", &a, &b); return a+b; }
Output the result
Calculate the sum of two numbers: Please enter two numbers 10 24 34
Example2: This program calculates the area of the square
#include<stdio.h> int sum(); void main() { printf("Calculate the area of this square\n"); float area = square(); printf("Area of the square: %f\n", area); } int square() { float side; printf("Please enter the length (in meters): "); scanf("%f", &side); return side * side; }
Output the result
Calculate the area of this square Area of the square: 10 Please enter the length (in meters): 100.000000
Example1
#include<stdio.h> void sum(int, int); void main() { int a, b, result; printf("\nCalculate the sum of two numbers:"); printf("\nPlease enter two numbers:"); scanf("%d %d", &a, &b); sum(a, b); } void sum(int a, int b) { printf("\nSum is: %d", a+b); }
Output the result
Calculate the sum of two numbers: Please enter two numbers 10 24 Sum is 34
Example2: This program calculates the average of five numbers.
#include<stdio.h> void average(int, int, int, int, int); void main() { int a, b, c, d, e; printf("\nTo calculate the average of five numbers:"); printf("\nPlease enter five numbers:"); scanf("%d %d %d %d %d", &a, &b, &c, &d, &e); average(a, b, c, d, e); } void average(int a, int b, int c, int d, int e) { float avg; avg = (a+b+c+d+e)/5; printf("Average of five numbers: %f", avg); }
Output the result
To calculate the average of five numbers: Please enter five numbers:10 20 30 40 50 Average of five numbers: 30.000000
Example1
#include<stdio.h> int sum(int, int); void main() { int a, b, result; printf("\nCalculate the sum of two numbers:"); printf("\nPlease enter two numbers:"); scanf("%d %d", &a, &b); result = sum(a, b); printf("\nSum is: %d", result); } int sum(int a, int b) { return a+b; }
Output the result
Calculate the sum of two numbers: Enter two numbers:10 20 The sum is : 30
Example2:Program to check if a number is even or odd
#include<stdio.h> int even_odd(int); void main() { int n, flag = 0; printf("\nCheck if a number is even or odd"); printf("\nEnter a number: "); scanf("%d", &n); flag = even_odd(n); if(flag == 0) { printf("\nThe number is odd"); } else { printf("\nThis number is even"); } } int even_odd(int n) { if(n%2 == 0) { return 1; } else { return 0; } }
Output the result
Check if a number is even or odd Enter a number: 100 This number is even
Standard library functions are built-in functions in the C language, which are grouped and placed in a public location called a library. These functions are used to perform certain specific operations. For example, printf is a library function used to print on the console. Library functions are created by the compiler designer. All C standard library functions are in the extension name.hare defined in different header files. We need to include these header files in the program to utilize the library functions defined in such header files. For example, to use functions like printf / Library functions like scanf, we need to include stdio.h in the program, which is a header file containing information about standard input/All output library functions.
The following table lists the most commonly used header files.
Serial number | Header file | Description |
---|---|---|
1 | stdio.h | This is a standard input/Output header file. It contains information about standard input/All output library functions. |
2 | conio.h | This is a console input/Output header file. |
3 | string.h | It contains all library functions related to strings, such as gets(), puts(), and so on. |
4 | stdlib.h | This header file contains all general library functions, such as malloc(), calloc(), exit(), and so on. |
5 | math.h | This header file contains all functions related to mathematical operations, such as sqrt(), pow(), and so on. |
6 | time.h | This header file contains all functions related to time. |
7 | ctype.h | This header file contains all character handling functions. |
8 | stdarg.h | Variable argument functions are defined in this header file. |
9 | signal.h | All signal handling functions are defined in this header file. |
10 | setjmp.h | This file contains all jump functions. |
11 | locale.h | This file contains locale functions. |
12 | errno.h | This file contains error handling functions. |
13 | assert.h | This file contains diagnostic functions. |
Visit these pages for more information: