Functions in C

Challenge Inside! : Find out where you stand! Try quiz, solve problems & win rewards!

Learn via video course

C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
By Prateek Narang
Free
star5
Enrolled: 1000
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
Prateek Narang
Free
5
icon_usercirclecheck-01Enrolled: 1000
Start Learning

Overview

Functions are blocks of code defined in a program to perform a particular task. Functions just need to be defined once in the program and then can be used multiple times. This also improves code readability and modularity while also providing code reusability.

Introduction

Functions are just a block of code defined to perform a particular task. In this way, if we break a complex program into functions, where each function performs a much simpler task, the program will become simple to code and will improve readability. The best thing about functions is that they can be reused again and again throughout the program, whenever required. This also helps in reducing repetitive codes in our program. We will see more about the reusability of functions in C later in this article.

Why We Need Functions in C?

Now, you might wonder if we could have calculated the area in the main function itself, without creating a new function. What is the need for a function?

Functions help replace those repeated lines of code with just one function call with different parameters each time, leading to reduced redundancy in the program.

Functions provide code readability, by making the code compact and also provide code reusability. Instead of writing the same code again and again for different rectangles, I just have to call the function with changed parameters.

Also, functions help divide the program into parts, where each part performs a specific task, this makes the complete code easier to understand. Another advantage comes when we want our code to be modular. This means the entire code is separated into different blocks, where each is independent of the other and performs a different task. This helps in the easier implementation and debugging of the individual blocks. Declaring a function makes this possible. Different functions perform different tasks, we just need to call them with the required parameters.

Defining a Function

Defining a function means specifying the body inside the function. This will be the code that will be executed every time the function is called.

The syntax for defining a function in C is :

It is necessary to declare a function in C before we can call it for execution, but it can be defined later. This is because the compiler needs to know that there is a function of that name that has been declared in the program before we call the function for execution. So, we can first declare a function at the top of our program, and implement it later in the code. Conversely, we can also just write the declaration and define the code in the same place before calling it.

Function Declarations

Declaring a function means we need to declare in the code the return type of the function, the function name, and the parameters it will accept. Let us look at these in detail.

  • Return Type: Usually, the function calculates a value, and return it back to the main() program. This value is said to be the return value. We need to declare what type of variable will be returned from the function. This can be int, double, char, string, and even void if the function does not return anything.
  • Function Name: Just like variables, every function has a name. This has to be included in the function declaration. Note that the function name should be unique, just like variable names.
  • Parameters: Sometimes the function needs specific inputs to execute the code inside. These inputs are passed to the function to be used in the form of function parameters. A function can have more than one parameter.

The syntax for declaring a function in C is :

Here, return_type refers to the type of variable returned by the function, function_name refers to the name of the function, and p1 and p2 are the names of the variables being passed as a parameter.

Calling a Function

Now that we have declared and defined the function, we can execute the code written inside it anytime by just one line in C.

Here p1 and p2 are the parameters that were previously mentioned during the declaration of the function. By just this one line, all the code inside the function that we defined above will be executed.

Note: While calling a function, the same number of parameters should be given as input as in the declaration. Also, these parameters should have the same data type as in the declaration.

While calling, in some cases, the function returns the values into the main function.

Function Arguments and Return values

There are four different aspects of function calls, based on whether a function accepts arguments and/or returns a value. A function may accept no arguments and return no value, accept no arguments and return a value, accept arguments and return no value, or accept arguments and return a value namely:

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

How Function Works in C Programming?

The working of functions in C programming is the compiler transferring the control to the function definition block with the given parameters, whenever it encounters a function call in the program. Be it user-defined or standard library functions, their working is the same.

Working of Functions in C

When a code starts executing, the compiler executes codes line by line in the main() function. When it encounters a function call in a particular line, it transfers the control to the function definition. Apart from this, the compiler stores the address of this line, which is called the return address, so that when the function ends we can return to this line.

When we reach the end of the function, the control is transferred back to the line from where the function was called using the return address, and the code starts executing as normal again.

Types of Functions in C

Now that we have discussed the syntax of functions and why they're important, let’s take a look at the types of functions.

Depending on where the function has been defined, functions are of two types:

  1. Standard Library Functions
  2. User Defined functions

Standard Library Functions

These are the pre-defined functions. As the name suggests, these functions are pre-defined in the standard libraries. These libraries can be included by using the header files, and after that, we can call these pre-defined functions which perform specific tasks.

For example, you must have used the scanf() function to take user input in your code. But where is this function defined? scanf() is a standard library function, which is defined in the stdio.h header, which we include at the very top of our code. Hence, scanf() is a standard library function that is predefined in the stdio.h header, and we don't need to define it, we just need to include the stdio.h header in our code to be able to use it.

User Defined Functions in C

In the case of functions that are case-specific and hence are not defined in any header files, we will have to declare and define our own functions. This can be done by defining our own function using the syntax described in the previous sections. We can use user-defined functions to implement program-specific codes.

How user-defined function works?

Let us take an example of a program to calculate the sum of two integers using a user-defined function.

Input

Output

In the above code, the compiler starts from the beginning of the main method. When it encounters the function call, it transfers the control to the place where the function has been defined. So, then the code inside the parentheses, in the function definition is executed.

Hence, in this case, when the function is called, the compiler goes to where the function was defined and calculates the sum. When the compiler reaches the return statement in the function, it transfers the control back to where it is left in the main method. So, the returned integer gets stored in the ans variable in the main method, and the remaining code is executed normally.

Passing Parameters to Functions

Passing parameters to functions is a fundamental concept in programming that allows us to pass data to a function for processing. Parameters are variables that are defined within the parentheses of a function and used to store values that can be passed in when the function is called. These values can be of any data type, including strings, numbers, or even other objects. Once the values are passed in as arguments, the function can then use them to perform a specific operation or set of operations.

  1. Pass by Value - In this method of parameter passing, the values from actual parameters are copied into the formal function parameters. Therefore, any modifications made inside the function are not reflected in the caller's parameters.
  2. Pass by Reference - The actual parameters of the caller and those of the function refer to the same memory locations, which means that any alterations made within the function are reflected in the caller's actual parameters as well.

Important Points About Function in C

  • When a program starts running, the main() function is executed first, which should be present in every C program.
  • Functions need to be declared in the program before they are called, if they are not declared before calling them, the compiler assumes it to be a function that returns an integer, and will show an error if that is not the case in C.
  • Every function needs to have a return type. If the function doesn’t return anything, then its return type will be void.
  • When you call the function, it is important to pass the same number of parameters as in the declaration of the function.
  • If no parameters are declared in a C function, the compiler assumes any number of parameters can be passed to it. If you want to declare a function that has no parameters in C, you can declare it as return_type function_name(void){}.

:::

Advantages of Functions in C

  • The most important advantage of functions is that they allow code reusability. We don't need to write the same code, again and again, we can just declare a function and keep calling it whenever required.
  • Functions help divide the complete program into parts, where each part performs a specific task. This makes the code clean and easier to understand.
  • Functions help achieve code modularity, which means the entire code is separated into different blocks, where each is independent of the other and performs a different task. This helps in the easier implementation and debugging of the individual blocks.
  • You can write new code in a function without disturbing or altering the previously written code in the main program.

Conclusion

  • Functions are blocks of code defined to perform a particular task.
  • A function consists of a return type, a function name, and a list of parameters.
  • We explored two types of functions in C :
    • Standard Library Functions
    • User-Defined Functions
  • The main() function is a special kind of function, which is compulsory in every C program, and the code starts executing from this function.
  • We looked at recursive functions, which are those functions that call themselves.
  • We also looked at the inline function, where all of the code written inside it will be pasted in the main() function and then it will be executed.