Functions in C

Learn via video course
FREE
View all courses
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
Topics Covered

Functions in C simplify coding by breaking complex programs into smaller, manageable blocks, each handling a specific task. This enhances readability and allows for code reuse, reducing repetition and making programs more efficient. The reusability aspect of functions, especially in C, will be explored further in this article.

Syntax of Function in C

The syntax of function in C can be broken down into three main aspects:

  1. Function Declaration
  2. Function Definition
  3. Function Calls

Function Declaration in C

Function Declarations:

  • Return Type: Specifies the type of value a function returns (int, double, char, string, void).
  • Function Name: Unique identifier for the function.
  • Parameters: Inputs required by the function; can be multiple.

Syntax:

Function Definition in C

Function definition in C 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.

Function Call in C

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 Function call in C, in some cases, the function returns the values into the main function.

Example of Function in C

This example includes a simple function to calculate the sum of two integers.

Code

Output

Return Type of Function in C

  • The return type of a function in C indicates the data type of the value returned to the caller.
  • Declared at the start of the function definition.
  • Use void for functions not returning a value.
  • For returning a value, specify the data type (int, float, char, etc.).
  • The return statement in the function body is used to return the value.

Arguments of Functions in C

  • Arguments are the values passed to a function when it is called, enabling parameterized operations.
  • Specified within the parentheses in the function declaration and definition.
  • Serve as input to the function to perform tasks or calculations.
  • Can be of any data type (e.g., int, float, char) and number.
  • Functions can also be designed without arguments, indicated by empty parentheses.

Conditions of Return Types and Arguments

  • Return types and arguments must match their declared data types.
  • A function's return type defines the type of value it sends back to the caller.
  • Arguments passed to a function must align with the types specified in the function's declaration.
  • Functions declared with void return type should not return a value.
  • Functions expecting arguments cannot be called without passing the appropriate number and type of arguments, unless designed to accept variable arguments or defaults.
  • The return statement's value must match the function's declared return type.

How Does C Function Work?

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 in c.

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

  1. Library Functions
  2. User Defined functions

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

User-defined functions are created to perform specific tasks not covered by standard library functions. These functions are declared and defined by the programmer to execute unique program requirements.

Example

The following Function in c example demonstrates calculating the sum of two integers using a user-defined function.

Input

Output

This code demonstrates how, upon calling sum(a, b) in main(), the compiler executes the sum function, computes the sum of a and b, and returns the result. The control then goes back to main(), where the result is printed.

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.

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.

FAQs

Q: How do functions in C enhance program structure?

A: Functions in C organize code into manageable segments, improving clarity and enabling code reuse, thereby making programs more efficient and easier to maintain.

Q: Can you explain the significance of specifying a return type in C functions?

A: The return type in C functions is crucial as it dictates the type of value the function will return to its caller, ensuring type safety and clarity in what the function outputs.

Q: How does C handle functions that do not return any value?

A: For functions that do not return a value, C uses the void return type, indicating that the function will perform an action without sending a value back to its caller.

Conclusion

  • Functions in C break complex programs into smaller tasks for improved readability and efficiency, with a focus on code reuse to minimize repetition.
  • They consist of a declaration to inform the compiler of their existence, a definition to outline the task they perform, and can be called within programs to execute their defined actions.
  • User-defined functions allow for the customization of tasks not available in standard libraries, providing flexibility and specificity in programming.
  • Parameters enable functions to receive input values, and the return type determines the nature of the output, enhancing functionality and interactivity within C programs.