Programmer Coding

Function in C Language

What is Function?

A function in C is a group of statements that perform a specific task when called. It is the fundamental building block of C programming, ensuring modularity and reusability of code. Descriptions of functions are enclosed in curly brackets { }, they have a meaning and perform specific functions. In other languages, these are also called subroutines or procedures.

C function syntax

The syntax of functions can be divided into 3 types:

  • function declaration
  • function definition
  • Function Calls

 

  • Function decleration

We must provide the function name, return type, number and type parameters in the function declaration. A function declaration informs the programmer that a function with the same name has been defined elsewhere in the program.

Syntax

return_type function_name(parameter1_type parameter1, parameter2_type parameter2, …);

Example

// Function declaration

int add(int num1, int num2);

  • function definition

The function definition contains the actual statements that are executed when the function is called (i.e., when the control service reaches the function).

C functions are usually defined and declared in one step, because function definition always starts with the function declaration, so we do not need to declare it explicitly. The following examples serve as both definitions and declarations.

Syntax

return_type function_name(parameter1_type parameter1, parameter2_type parameter2, …) {

// Function body

// Statements defining the functionality of the function

}

Example

// Function definition

int add(int num1, int num2) {

int sum = num1 + num2;

return sum;

}

  • Function Calls

A function call is a statement that tells the compiler to execute a function. We use function names and parameters in function calls.

In the example below, the first function is called and 10,30 is transferred to the sum function. After the return function is called between a and b, control returns to the main function of the program.

Syntex

return_type variable_name = function_name(argument1, argument2, …);

Example

#include <stdio.h>

int main() {

int result;

// Function call

result = add(7, 3);

// Output the result

printf(“Result of addition: %d\n”, result);

return 0;

}

Output

Result of addition: 10

Function Return Type

Function return type tells what type of value is returned after all function is executed. When we don’t want to return a value, we can use the void data type.

Example

Example

int func(parameter_1,parameter_2);

Function ArgumentsFunction Arguments (also known as Function Parameters) are the data that is passed to a function.

Example

int function_name(int var1, int var2);

Conditions of Return Types and Arguments

In the C programming language, a function can be called with or without parameters and a value can be returned. They may or may not return a value to the calling function.

  • Function with no arguments and no return value·
  • Function with no arguments and with return value·
  • Function with argument and with no return value·
  • Function with arguments and with return value

Types of Functions

There are two types of functions in C: 

1.  Library Functions

2.  User Defined Functions

1.  Library Functions

Library functions are predefined functions provided by standard C libraries. These functions are ready to use and cover a wide range of functionalities such as input/output operations, mathematical calculations, string manipulation, memory allocation, etc.

Syntax

#include <library_name.h>

int main() {

// Function call

return_type variable_name = library_function_name(argument1, argument2, …);

return 0;

}

Example

#include <stdio.h> // Include the standard input/output library

int main() {

// Function call to printf() for output

printf(“programmercoding.com!\n”);

return 0;

}

Output

programmercoding.com!

2.  User Defined Functions

User-defined functions are functions created by the programmer to perform specific tasks as needed. These functions provide modularity, code reusability, and ease of maintenance in the program.

Syntax

return_type function_name(parameter1_type parameter1, parameter2_type parameter2, …) {

// Function body

// Statements defining the functionality of the function

}

Example

#include <stdio.h>

// Function declarationint

add(int num1, int num2);

int main() {

int result;

// Function call

result = add(5, 3);

// Output the result

printf(“Result of addition: %d\n”, result);

return 0;

}

// Function definition

int add(int num1, int num2) {

int sum = num1 + num2;

return sum;

}

Output

Result of addition: 8

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top