Detailed Explanation of Functions in C Language


In C language, we can break down a large program into basic building blocks called functions. A function contains a set of programming statements enclosed in {}. Functions can be called multiple times to provide reusability and modularity in C programs. In other words, we can say that a collection of functions creates a program. Functions are also known as procedures or subroutines in other programming languages.
The advantages of C functions are as follows:
  • By using functions, we can avoid rewriting the same logic/code multiple times in the program.

  • We can call C functions multiple times from anywhere in the program.

  • When breaking down a large C program into multiple functions, it becomes easier to track the program.

  • Reusability is the main advantage of C functions.

  • However, there is always some overhead associated with function calls in C programs.

Three Aspects of Functions

C functions have three aspects.

  • Function Declaration: Functions must be globally declared in a C program to inform the compiler about the function name, function parameters, and return type.

  • Function Call: Functions can be called from anywhere in the program. The parameter list of a function call must match that of the function declaration. In a function call, we must pass the same number of parameters as declared in the function declaration.

  • Function Definition: It contains the actual statements to be executed. When a function is called, the control flow enters this most important aspect. Here, we must note that only one value can be returned from a function.

Index Aspect of C Function Syntax
1 Function Declaration ReturnType FunctionName(ParameterList);
2 Function Call FunctionName(ParameterList)
3 Function Definition ReturnType FunctionName(ParameterList) {FunctionBody;}

The syntax for creating a function in C language is as follows:

ReturnType FunctionName(DataType Parameter...) {// Code to be executed}

Types of Functions

There are two types of functions in C programs:

  1. Library Functions: These functions are declared in C header files, such as scanf(), printf(), gets(), puts(), ceil(), floor(), etc.

  2. User-defined Functions: These functions are created by C programmers for reuse. They reduce the complexity of large programs and optimize the code.

Detailed Explanation of Functions in C Language

Return Values

C functions can have return values or not. If a function does not need to return any value, use void as the return type.

Let’s look at a simple example of a C function that does not return a value.

Example of a Function Without Return Value:

#include <stdio.h>
void hello() {  printf("hello c");}
int main() {  hello();  return 0;}
// Output: // hello c

If you want to return any value from a function, you need to use data types such as int, long, char, etc. The return type depends on the value that the function is supposed to return.

Let’s look at a simple C function example that returns an int value from the function.

👇Click to Claim👇
👉C Language Knowledge Resource Collection

Example of a Function With Return Value:

#include <stdio.h>
int get() {  return 10;}
int main() {  int result = get();  printf("%d", result);  return 0;}
// Output: // 10

In the above example, we return the value 10, so the return type is int.If you want to return a floating-point value (for example, 10.2, 3.1, 54.5, etc.), you need to use float as the return type of the method.

#include <stdio.h>
float get() {  return 10.2;}
int main() {  float result = get();  printf("%f", result);  return 0;}
// Output: // 10.200000

Now, you need to call the function to get the value from the function.

Different Aspects of Function Calls

A function can accept any number of parameters or none at all. It can return any value or no value at all. Based on these facts, there are four different aspects of function calls.

  • Function without parameters and without return value

  • Function without parameters and with return value

  • Function with parameters and without return value

  • Function with parameters and with return value

Example of Function Without Parameters and Without Return Value

Example 1:

#include <stdio.h>
void printName();
void main() {  printf("Hello ");  printName();}
void printName() {  printf("Javatpoint");}
// Output: // Hello Javatpoint
Output
Hello Javatpoint

Example 2:

#include <stdio.h>
void sum();
void main() {  printf("\nGoing to calculate the sum of two numbers:");  sum();}
void sum() {  int a, b;  printf("\nEnter two numbers: ");  scanf("%d %d", &a, &b);  printf("The sum is %d", a + b);}
// Output: // Going to calculate the sum of two numbers://// Enter two numbers: 10// 24//// The sum is 34

Output

Going to calculate the sum of two numbers:
Enter two numbers: 10 24
The sum is 34

Example of Function Without Parameters and With Return Value

Example 1

#include <stdio.h>
int sum();
void main(){int result;  printf("\nGoing to calculate the sum of two numbers:");result = sum();printf("%d",result);}
int sum(){int a,b;  printf("\nEnter two numbers");scanf("%d %d",&a,&b);return a+b;  }

Output

Going to calculate the sum of two numbers:
Enter two numbers: 10 24
The sum is 34

Example 2: Calculate the Area of a Square

#include <stdio.h>
int square();
void main(){printf("Going to calculate the area of the square\n");float area = square();printf("The area of the square: %f\n",area);}
int square(){float side;printf("Enter the length of the side in meters: ");scanf("%f",&side);return side * side;}

Output

Going to calculate the area of the square
Enter the length of the side in meters: 10
The area of the square: 100.000000

Example of Function With Parameters and Without Return Value

Example 1

#include <stdio.h>
void sum(int, int);
void main(){int a,b,result;  printf("\nGoing to calculate the sum of two numbers:");printf("\nEnter two numbers:");scanf("%d %d",&a,&b);sum(a,b);}
void sum(int a, int b){printf("\nThe sum is %d",a+b);  }

Output

Going to calculate the sum of two numbers:
Enter two numbers: 10 24
The sum is 34

Example 2: Calculate the Average of Five Numbers

#include <stdio.h>
void average(int, int, int, int, int);
void main(){int a,b,c,d,e;  printf("\nGoing to calculate the average of five numbers:");printf("\nEnter 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("The average of given five numbers : %f",avg);}

Output

Going to calculate the average of five numbers:
Enter five numbers: 10 20 30 40 50
The average of given five numbers : 30.000000

Example of Function With Parameters and With Return Value

Example 1

#include <stdio.h>
int sum(int, int);
void main(){int a,b,result;  printf("\nGoing to calculate the sum of two numbers:");printf("\nEnter two numbers:");scanf("%d %d",&a,&b);result = sum(a,b);printf("\nThe sum is : %d",result);}
int sum(int a, int b){return a+b;}

Output

Going to calculate the sum of two numbers:
Enter two numbers: 10 20
The sum is : 30

Example 2: Check Whether a Number is Odd or Even

#include <stdio.h>
int even_odd(int);
void main(){int n,flag=0;printf("\nGoing to check whether a number is even or odd");printf("\nEnter the number: ");scanf("%d",&n);flag = even_odd(n);if(flag == 0){  printf("\nThe number is odd");}else  {  printf("\nThe number is even");}}
int even_odd(int n){if(n%2 == 0){  return 1;}else  {  return 0;}}

Output

Going to check whether a number is even or odd
Enter the number: 100
The number is even

C Library Functions

C library functions are built-in functions in the C language, grouped and placed in a common location called a library. These functions are used to perform specific operations. For example, printf is a library function used to print output to the console. C library functions are created by the designers of the compiler. All standard C library functions are defined in different header files with the .h extension. We need to include these header files in our program to use the library functions defined in them. For example, to use library functions like printf/scanf, we need to include stdio.h, which is a header file that contains all library functions related to standard input/output.

The following table lists commonly used header files:

Index Header File Description
1 stdio.h This is a standard input/output header file. It contains all library functions related to standard input/output.
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(), etc.
4 stdlib.h This header file contains all general library functions, such as malloc(), calloc(), exit(), etc.
5 math.h This header file contains all functions related to mathematical operations, such as sqrt(), pow(), etc.
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 parameter 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.

Detailed Explanation of Functions in C LanguageProgrammer Technical Exchange GroupDetailed Explanation of Functions in C Language

Scan the code to enter the group and remember to note: city, nickname, and technical direction.




Leave a Comment