What is a function in a program?

Before jumping to the function call, we need to understand the function in the C programming language. A function is a group of tasks used to execute the predefined operations and returns a value. A large program can be divided into small blocks of code that help to understand the logic, debug, and modified it.

What is a function in a program?

In the C programming language, the function is divided into two parts: the built-in/ library function and the user-defined function.

Library/ Built-in Function

A library function is predefined functions, and its tasks are also defined in the C header files. So, it does not require writing the code of the particular function; instead, it can be called directly in a program whenever it is required. Example: printf(), scanf(), getch(), etc., are the predefined function in the C library, and the meaning of these functions cannot be changed.

Let's write a program to define the library function in C.

Predefined.c

Output:

Welcome to the JavaTpoint.com
It is the library function or built-in function in C

User-defined Function

It is a user-defined function in the C programming language to execute some specific actions according to the programmer's requirement. A user-defined function is divided into three types such as function declaration, function definition, and function call.

Function Declaration

A function declaration defines the name and return type of a function in a program. Before using the function, we need to declare it outside of a main() function in a program.

Syntax:

Example of function declaration:

In the above example, int is a return data type of the function name add function that contains two integer parameters as num1 and num2. Furthermore, we can write the above function declaration is as follows:

Function Definition

It defines the actual body of a function inside a program for executing their tasks in C.

Syntax:

In the above syntax, a function definition contains the three parts as follow:

  1. Return Data_Type: It defines the return data type of a value in the function. The return data type can be integer, float, character, etc.
  2. Function Name: It defines the actual name of a function that contains some parameters.
  3. Parameters/ Arguments: It is a parameter that passed inside the function name of a program. Parameters can be any type, order, and the number of parameters.
  4. Function Body: It is the collection of the statements to be executed for performing the specific tasks in a function.

Consider an example to demonstrate the function definition:

Function Calling:

A function call is an important part of the C programming language. It is called inside a program whenever it is required to call a function. It is only called by its name in the main() function of a program. We can pass the parameters to a function calling in the main() function.

Syntax:

Let's consider a program to call a function in C programming languages.

Add.c

Output:

Enter the first and second number
5
6
The sum of the two number is 11

Call by Value:

When single or multiple values of an actual argument are copied into the formal parameter of a function, the method is called the Call by Value. Hence, it does not alter the function's actual parameter using the formal parameter.

Consider a program to demonstrate the Call by Value in C programming.

Call_Value.c

Output:

x = 10, y = 20 from main before calling the function
x = 15, y = 25 from modular function
 x = 10, y = 20 from main after calling the function

Call by Reference:

In this method, the address of the actual argument is copied into the function call's formal parameter; the method is known as Call by Reference. If we make some changes in the formal parameters, it shows the effect in the value of the actual parameter.

Consider a program to demonstrate the Call by Reference in C programming.

Call_Ref.c

Output:

x = 10, y = 20 from main before calling the function
x = 15, y = 25 from modular function
 x = 15, y = 25 from main after calling the function


next → ← prev

In c, we can divide a large program into the basic building blocks known as function. The function contains the set of programming statements enclosed by {}. A function can be called multiple times to provide reusability and modularity to the C program. In other words, we can say that the collection of functions creates a program. The function is also known as procedureor subroutinein other programming languages.

Advantage of functions in C

There are the following advantages of C functions.

  • By using functions, we can avoid rewriting same logic/code again and again in a program.
  • We can call C functions any number of times in a program and from any place in a program.
  • We can track a large C program easily when it is divided into multiple functions.
  • Reusability is the main achievement of C functions.
  • However, Function calling is always a overhead in a C program.

Function Aspects

There are three aspects of a C function.

  • Function declaration A function must be declared globally in a c program to tell the compiler about the function name, function parameters, and return type.

  • Function call Function can be called from anywhere in the program. The parameter list must not differ in function calling and function declaration. We must pass the same number of functions as it is declared in the function declaration.

  • Function definition It contains the actual statements which are to be executed. It is the most important aspect to which the control comes when the function is called. Here, we must notice that only one value can be returned from the function.

SNC function aspectsSyntax
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 of creating function in c language is given below:

Types of Functions

There are two types of functions in C programming:

  1. Library Functions: are the functions which are declared in the C header files such as scanf(), printf(), gets(), puts(), ceil(), floor() etc.
  2. User-defined functions: are the functions which are created by the C programmer, so that he/she can use it many times. It reduces the complexity of a big program and optimizes the code.
What is a function in a program?

Return Value

A C function may or may not return a value from the function. If you don't have to return any value from the function, use void for the return type.

Let's see a simple example of C function that doesn't return any value from the function.

Example without return value:

If you want to return any value from the 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 see a simple example of C function that returns int value from the function.

Example with return value:

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

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

Different aspects of function calling

A function may or may not accept any argument. It may or may not return any value. Based on these facts, There are four different aspects of function calls.

  • function without arguments and without return value
  • function without arguments and with return value
  • function with arguments and without return value
  • function with arguments and with return value

Example for Function without argument and return value

Example 1

Output

Hello Javatpoint

Example 2

Output

Going to calculate the sum of two numbers:

Enter two numbers 10 
24 

The sum is 34

Example for Function without argument and with return value

Example 1

Output

Going to calculate the sum of two numbers:

Enter two numbers 10 
24 

The sum is 34

Example 2: program to calculate the area of the square

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 for Function with argument and without return value

Example 1

Output

Going to calculate the sum of two numbers:

Enter two numbers 10 
24 

The sum is 34

Example 2: program to calculate the average of five numbers.

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 for Function with argument and with return value

Example 1

Output

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

Example 2: Program to check whether a number is even or odd

Output

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

C Library Functions

Library functions are the inbuilt function in C that are grouped and placed at a common place called the library. Such functions are used to perform some specific operations. For example, printf is a library function used to print on the console. The library functions are created by the designers of compilers. All C standard library functions are defined inside the different header files saved with the extension .h. We need to include these header files in our program to make use of the library functions defined in such header files. For example, To use the library functions such as printf/scanf we need to include stdio.h in our program which is a header file that contains all the library functions regarding standard input/output.

The list of mostly used header files is given in the following table.

SNHeader fileDescription
1 stdio.h This is a standard input/output header file. It contains all the library functions regarding standard input/output.
2 conio.h This is a console input/output header file.
3 string.h It contains all string related library functions like gets(), puts(),etc.
4 stdlib.h This header file contains all the general library functions like malloc(), calloc(), exit(), etc.
5 math.h This header file contains all the math operations related functions like sqrt(), pow(), etc.
6 time.h This header file contains all the time-related functions.
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 the signal handling functions are defined in this header file.
10 setjmp.h This file contains all the 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 diagnostics functions.

Next TopicCall by value and call by reference in C

← prev next →

What is a function in programming example?

Every programming language lets you create blocks of code that, when called, perform tasks. Imagine a dog that does the same trick only when asked. Except you do not need dog treats to make your code perform. In programming, these code blocks are called functions.

What is function in C programming with examples?

There are two types of functions in C programming: Library Functions: are the functions which are declared in the C header files such as scanf(), printf(), gets(), puts(), ceil(), floor() etc. User-defined functions: are the functions which are created by the C programmer, so that he/she can use it many times.

What do you mean by function?

A function is defined as a relation between a set of inputs having one output each. In simple words, a function is a relationship between inputs where each input is related to exactly one output. Every function has a domain and codomain or range.

How do you know if a program is a function?

To determine whether a relation is a function, you need to check whether one input value leads to two different output values. If one input value does lead to two different output values, you will be able to tell visually because the two points will line up vertically.