Table of contents
1.
Introduction
2.
Advantages of Functions in C
2.1.
Reusability
2.2.
Easy to Test
2.3.
Modularity
2.4.
Efficiency in Debugging
3.
Function Aspects in C
3.1.
Definition
3.2.
Declaration
3.3.
Call
3.4.
Parameters
3.5.
Return Type
4.
Types of Functions in C
4.1.
Functions Without Arguments & Without Return Value
4.2.
Functions With Arguments & Without Return Value
4.3.
Functions Without Arguments & With Return Value
4.4.
Functions With Arguments & With Return Value
5.
Return Value in C Functions
6.
Different Aspects of Function Calling in C
6.1.
No Arguments & No Return Value
6.2.
With Arguments & No Return Value
6.3.
No Arguments & With Return Value
6.4.
With Arguments & With Return Value
7.
Example for Function Without Argument & Without Return Value
7.1.
C
8.
Example for Function Without Argument & With Return Value
8.1.
C
9.
Example for Function with Argument & Without Return Value
9.1.
C
10.
Example for Function with Argument & With Return Value
10.1.
C
11.
C Library Functions
11.1.
printf() and scanf()
11.2.
String Functions
11.3.
Math Functions
11.4.
Memory Functions
12.
Frequently Asked Questions
12.1.
Can a function return more than one value in C?
12.2.
Is it necessary to declare a function before calling it in C?
12.3.
What happens if a function doesn't have a return statement?
13.
Conclusion
Last Updated: Aug 20, 2024
Easy

Types of Functions in C

Author Gaurav Gandhi
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Functions in C are essential tools that allow programmers to break down complex problems into simpler, more manageable tasks. Think of them as individual tasks or actions your program can perform. From performing calculations to processing data, functions help keep your code organized and reusable. 

Types of Functions in C

In this article, we'll learn into what functions are, their importance in C programming, and the different types you'll encounter. We’ll learn about different type of functions and how to use them.

Advantages of Functions in C

Simplifies Code: Functions break down complex code into smaller, easier-to-manage sections. This makes your code simpler to read & understand.

Reusability

Once you write a function, you can use it multiple times. This means you don't have to write the same code over & over again.

Easy to Test

With functions, you can test small parts of your code separately. This makes finding & fixing errors easier.

Modularity

Functions allow you to build your program in modules or pieces. This helps in organizing your code better.

Efficiency in Debugging

If there's a problem, you can check it within a specific function. This makes the debugging process much quicker & more efficient.

Function Aspects in C

When we talk about functions in C, there are a few key aspects to keep in mind:

Definition

This is where you describe what the function does & how. It includes the code that runs when the function is called.

Declaration

This tells the compiler about a function's name, return type, & parameters (if any). It's like giving a heads up to the compiler that this function exists.

Call

This is when you use the function in your program. By calling a function, you're telling the program to execute the code defined in that function.

Parameters

These are the values you pass to the function when you call it. Functions can use these values to perform tasks.

Return Type

This is the type of value your function gives back after it finishes. If your function doesn't need to return anything, its type is void.

Types of Functions in C

In C programming, functions are categorized based on whether they take inputs (arguments) and whether they give back a result (return value). Like -: 

Functions Without Arguments & Without Return Value

These functions do not take any input and do not return any value. They're used for tasks that don't require input and don't need to send back a result.

Functions With Arguments & Without Return Value

These functions take input, perform a task with it, but do not return any value. They're useful for actions that require input but don't produce a direct result.

Functions Without Arguments & With Return Value

These functions don't take any input but do return a value. They can be used for operations that always give the same result without needing any external information.

Functions With Arguments & With Return Value

These are the most versatile functions. They take input, perform a task with it, and return a result. They're great for tasks where you need to process input and use the result in your program.

Return Value in C Functions

Every function in C is designed to do a task. Once the task is done, the function can send back a result to the part of the program that called it. This result is known as the return value. Not all functions need to return something. For those that do, the return value can be any data type, like an integer, a floating-point number, or even a character.

Here's how it works:

  • When you define a function, you decide whether it will return a value. This is specified in the function's definition by choosing a data type for the return value, like int for integers, float for floating-point numbers, etc.
     
  • Inside the function, the return statement is used to send the result back to the caller.
     
  • If a function doesn't need to return anything, you use the word void in the function's definition. This tells the compiler that the function won't return any value.
     
  • The return value feature is a powerful aspect of functions, enabling your programs to be more modular and reusable by allowing functions to communicate results back to their callers.

Different Aspects of Function Calling in C

Calling a function in C means telling the program to execute the set of instructions defined in that function. When you call a function, you might need to provide some information to it, known as arguments, and you might expect some information back, known as the return value. Here are the key points about function calling:

No Arguments & No Return Value

You just mention the function name followed by parentheses. Example: functionName();. This tells the program to run the function without needing any extra information, and you don't expect anything back.

With Arguments & No Return Value

When calling such a function, you provide the needed information inside the parentheses. Example: functionName(x, y);. This passes the values of x and y to the function, which uses them to perform its task. The function itself doesn't send any result back.

No Arguments & With Return Value

You call the function by its name with empty parentheses, but this time you might want to store the result it returns. Example: result = functionName();. This will run the function and save its return value in result.

With Arguments & With Return Value

Here, you provide arguments and also capture the return value. Example: result = functionName(x, y);. This passes x and y to the function and stores the returned value in result.

Example for Function Without Argument & Without Return Value

We'll create a function that doesn't need any input (no arguments) and doesn't give anything back (no return value). This type of function is great for doing a task where you don't need any outside information, and you're not waiting for a result.

Here's a basic function that just prints a message:

  • C

C

#include <stdio.h>

// Function declaration

void printMessage();

int main() {

   // Function call

   printMessage();

   return 0;

}

// Function definition

void printMessage() {

   printf("Hello, I am a simple function!\n");

}
You can also try this code with Online C Compiler
Run Code

Output

Hello, I am a simple function!


In this example:

void printMessage() is the function declaration & definition. void means it doesn't return any value.

Inside main(), printMessage(); is where we call the function. When this line runs, the program jumps to the printMessage function, prints the message, and then comes back to continue any code after the function call.

Example for Function Without Argument & With Return Value

Now, let's look at a function that doesn’t need any input from us (no arguments) but gives us something back (a return value). This kind of function is useful when you need to get a specific piece of information or result that doesn't depend on external values.

Consider a function that always returns the same number:

  • C

C

#include <stdio.h>

// Function declaration
int giveNumber();

int main() {
int number;

// Function call and storing the return value
number = giveNumber();

printf("The number is: %d\n", number);
return 0;
}

// Function definition
int giveNumber() {
return 5; // This function always returns 5
}
You can also try this code with Online C Compiler
Run Code

Output

The number is: 5

In this example:

int giveNumber() declares a function that will return an integer value.

In main(), the line number = giveNumber(); calls the giveNumber function, which executes and returns the value 5. This value is then stored in the variable number.

The printf statement prints the returned value, showing it in the console.

Example for Function with Argument & Without Return Value

Now, let's explore a function that needs some information from us (arguments) but doesn't need to send anything back (no return value). This type of function is handy for tasks where you need to provide some data, and the function uses this data to perform an action, but you don't need any information in return.

Here's how you can create a function that prints a number given to it:

  • C

C

#include <stdio.h>

// Function declaration
void printNumber(int num);

int main() {
// Calling the function with an argument
printNumber(7);
return 0;
}

// Function definition
void printNumber(int num) {
printf("The number is: %d\n", num);
}
You can also try this code with Online C Compiler
Run Code

Output

The number is: 7


In this code:

void printNumber(int num) is the function that takes one integer num as an argument. void indicates it doesn't return a value.

When we call this function in main() using printNumber(7);, we're passing the value 7 to the function.

The function then uses this value in its printf statement to display "The number is: 7".

Example for Function with Argument & With Return Value

This is a function that not only requires some input (arguments) but also gives us a result back (return value). This kind of function is very useful for tasks where you need to process some input data and then use the outcome in your program.

Imagine we have a function that takes two numbers, adds them together, and then returns the sum:

  • C

C

#include <stdio.h>

// Function declaration
int addNumbers(int a, int b);

int main() {
int result;

// Function call with arguments and capturing the return value
result = addNumbers(5, 3);

printf("The sum is: %d\n", result);
return 0;
}

// Function definition
int addNumbers(int a, int b) {
int sum = a + b; // Adding the two numbers
return sum; // Returning the sum to the caller
}
You can also try this code with Online C Compiler
Run Code

Output

The sum is: 8

 

Here's what's happening:

int addNumbers(int a, int b) declares a function that takes two integers as input and returns an integer.

Inside main(), we call addNumbers(5, 3), providing two numbers as input. The function processes these numbers, adds them, and returns their sum.

The returned sum is stored in result, and we print it out.

C Library Functions

C comes with a standard library packed with pre-defined functions that you can use in your programs. These functions cover a wide range of tasks, from input/output operations to string handling, math calculations, and more. 

Using library functions can save you a lot of time, as you don't need to write code from scratch for common tasks.

Here are a few examples of commonly used C Library Functions:

printf() and scanf()

You've already seen printf() in our examples, which is used for output. Similarly, scanf() is used for input. These functions make it easy to display messages and get user input.

String Functions

Functions like strcpy(), strlen(), and strcat() are used for string operations. For example, strlen() gives you the length of a string.

Math Functions

C's math library (math.h) offers functions like pow(), sqrt(), and sin(). For instance, pow(x, y) calculates the value of x raised to the power of y.

Memory Functions

Functions like malloc() and free() are used for dynamic memory allocation. malloc() allocates a specified number of bytes and returns a pointer to the first byte.

Using these library functions not only makes your programming task easier but also helps keep your code clean and efficient. They are well-tested and optimized, ensuring reliability and performance in your programs.

Frequently Asked Questions

Can a function return more than one value in C?

Directly, no. A function in C can return only one value. However, you can use pointers, arrays, or structures to effectively return multiple values from a function.

Is it necessary to declare a function before calling it in C?

Yes, it's generally good practice to declare a function before calling it. This declaration informs the compiler about the function's return type and its parameters, ensuring the function is used correctly.

What happens if a function doesn't have a return statement?

In functions declared with a return type other than void, omitting a return statement can lead to undefined behavior. For void functions, a return statement is not required.

Conclusion

In conclusion, the types of functions in C are user-defined, standard library, and those based on return type and arguments is essential for writing efficient and modular code. Functions allow you to break down complex tasks, enhance readability, and promote code reuse. Mastery of these concepts is key to becoming proficient in C and building a strong programming foundation.

You can refer to our guided paths on the Coding Ninjas. You can check our course to learn more about DSADBMSCompetitive ProgrammingPythonJavaJavaScript, etc. Also, check out some of the Guided Paths on topics such as Data Structure and AlgorithmsCompetitive ProgrammingOperating SystemsComputer Networks, DBMSSystem Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.

Live masterclass