Everything You Need To Know About Functions in C?

Published on Sep 10,2019 3.1K Views


This article will introduce you a simple yet a very fundamental and important programming concept that is Functions in C and follow it up with a demonstration. Following pointers will be covered in this article,

Functions are building blocks of any programming language. In simple words, function in a set of statements, which takes inputs, perform a specific task & then returns the output.
The ideology behind creating function is to bind a set of related statement together which performs a specific task. So that, you don’t have to write the same code multiple times for different set of inputs. You just have to call the function for different inputs, it will perform the specified task for the given input & return the output. You can call the function as many times as you want. In this blog, we will learn each & every nuance about functions in C programming language.

Let us start with the most fundamental question.

What are Functions in C?

Functions are same in C as of any other programming language. It is a set of codes bind together to perform a specific task. The set of code to be executed is specified in curly braces, i.e. ‘{ }’.

Before learning how to write a function in C, let’s first understand what the advantages are.

Advantages of Functions in C

The advantages of functions are common across all the programming languages.
The main idea behind function is to reduce the redundancy in the code. Suppose you have a functionality that needs to be performed multiple times in a program, so instead of writing it multiple times, you can create a function for that task and call it as many times as you want. Another hidden benefit is, if the logic of your functionality changes afterwards, then you don’t have to go ahead and change it at multiple places. You just have to change the code at one place (i.e. in the function) & it would be reflected throughout the program.

Modularity in again an added benefit. Writing a big piece of code including each & everything, reduces the readability of the code & makes it difficult to manage. You can divide the code in sections for individual functionalities using functions, which is simpler to understand and easier to manage.

Function also provides abstraction, where we can call a function and get the output without knowing the internal implementation.

Moving on with types of Function C

Types of Function in C

There are two types of functions:
Library functions
User-defined functions

Library functions are those functions which are already defined in C library such as strcat(), printf(), scanf() etc. You just need to include appropriate header files to use these functions.
User-defined functions are those functions which are defined by the user. These functions are made for code reusability and for saving time and space.

Now that we know the benefits of creating a function let’s understand how to declare a function in C.

Function Declaration & Definition

Function Declaration:

Syntax of function declaration:

return_type function_name (data_type arg1, data_type arg2)
int add(int x, int y); //function declaration

In Function Declaration, we specify the name of the function, the number of input parameter, their datatypes & the return type of the function. Function declaration tells compiler about the list of arguments the function is expecting with their data types & the return type of the function.

In function declaration, specifying the names of the parameter is optional, but specifying their data types is mandatory.

int add(int, int); //function declaration

The above specified function will take two integer parameters.

Function Definition

Image- Functions in C- Edureka
int add(int, int); 
//function declaration 
return_type function_name(parameters) 
{ 
Function body 
}

As shown in the above image a function definition consists of two parts i.e. function header & function body

Function Header: function header is same as function declaration without the semicolon. Function header contains function name, parameter & return type.

  • Return Type: Return type is the data type of the value which will be returned by the function. The function may or may not return a value. If it does then the data type of the retuning value should be specified, otherwise the return type needs to be void.

  • Function Name: This is the name of the function using which we can call the function when & where needed.

  • Parameters: The parameters are the input values which will be passed to the function. It tells about the data types of the arguments, their order and the number of arguments that will be passed to the function. The parameters are optional. You can also have functions without parameters.

Function Body: The function body is the set of statement which performs a specific task. It defines what the function does.

Example:

int add(int x, int y)
{
int sum = x+y;
return(sum);
}

It is recommended to declare a function before we define & use it. In C, we can declare & define the function at the same place.

Example:

#include <stdio.h>
int add(int, int); //function declaration
// function definition
int add(int x, int y) //function header
{
// function body
int sum = x+y;
return(sum);
}
// Main Function
int main()
{
int sum = add(23, 31);
printf("%d", sum);
return 0;
}

As we can see in the above example that we are calling the function using int sum = add(23, 31); statement. The returned value from the function is stored in sum variable.

Before we move ahead, there is one more important concept to understand about the parament. There are two types of parameter:

Actual Parameter: Those parameters which are passed to functions while calling them is are known as actual parameter. For example, 23 & 31 in the above example are the actual parameters.

Formal Parameter: Those parameters which are received by the functions are known as formal parameters. For example, x & y in the above example are the formal parameters.

Let’s quickly move ahead and understand the different ways of calling a function in C.

Calling a Function

There are two ways in which we can call a function:

  • Call by value
  • Call by reference

Call by value

In call by value method, the value of the actual parameter is passed as an argument to the function. The value of the actual parameter cannot be changed by the formal parameters.

In call be value method, different memory address is allocated to formal & actual parameters. Just the value of actual parameter is copied to formal parameter.

Example:

#include <stdio.h>
void Call_By_Value(int num1) 
{ 
num1=42; 
printf("nInside Function, Number is %d", num1);
} 
int main() 
{ 
int num; 
num=24; 
printf("nBefore Function, Number is %d", num); 
Call_By_Value(num); 
printf("nAfter Function, Number is %dn", num); 
return 0; 
}

Output

Output- Function in C- Edureka

 

In the above example, before call by value function, the value of num is 24. Then, once we call the function and pass the value, & change it inside the function, it becomes 42. When we come back & again print the value of num in main function, it becomes 24.

Call by reference

In call by reference, the memory address of the actual parameter is passed to the function as argument. Here, the value of the actual parameter can be changed by the formal parameter.

Same memory address is used for both the actual & formal parameter. So, if the value of formal parameter is modified, it is reflected by the actual parameter as well.

In C we use pointers to implement call by reference. As you can see in the below example, the function Call_By_Reference is expecting a pointer to an integer.

Now, this num1 variable will store the memory address of the actual parameter. So, to print the value which is stored in the memory address pointed by num1 we need to use dereference operator i.e. *.  So, the value of *num1 is 42.

The address operator & is used to get the address of a variable of any data type. So in the function call statement ‘Call_By_Reference(&num);’, the address of num is passed so that num can be modified using its address.

Example

#include <stdio.h>
// function definition
void Call_By_Reference(int *num1) 
{ 
*num1=42; 
printf("nInside Function, Number is %d", *num1);
} 
// Main Function
int main() 
{ 
int num; 
num=24; 
printf("nBefore Function, Number is %d", num); 
Call_By_Reference(&num); 
printf("nAfter Function, Number is %dn", num); 
return 0; 
}

Output 

Output- Function in C- Edureka

 

In this example, the value of num is 24 initially, inside the main function. Once it is passed to the Call_By_Reference function and the value is modified by the formal parameter, it got changed for the actual parameter as well. This is why when we are printing the value of num after the function it is printing 42.

Moving on with types of user-defined function in C

Types of User-Defined Function in C

There are various kinds of user-defined functions based of the return type & arguments passed.

Moving on with No arguments passed and no return value

1.No arguments passed and no return Value

Syntax:

function declaration:

void function();
function call: function();
function definition:
void function()
{
statements;
}

Example

#include <stdio.h>
void add(); 
void add() 
{
int x = 20;
int y = 30;
int sum = x+y;
printf("sum %d", sum);
}
int main()
{
add();
return 0;
}

Moving on with No arguments passed but a return value

2 No arguments passed but a return value

Syntax:

function declaration:

int function ( );
function call: function ( );
function definition:
int function( )
{
statements;
return a;
}

Example:

#include <stdio.h>
int add(); 
int add() 
{
int x = 20;
int y = 30;
int sum = x+y;
return(sum);
}
int main()
{
int sum;
sum = add();
printf("sum %d", sum);
return 0;
}

Moving on with Arguments passed but no return value

3 Argument passed but no return value

 Syntax:

function declaration:

void function ( int );
function call: function( a );
function definition:
void function( int a )
{
statements;
}

Example:

#include <stdio.h>
void add(int, int); 
void add(int x, int y) 
{
int sum = x+y;
return(sum);
}
int main()
{
add(23, 31);
return 0;
}

Moving on with Argument passed and a return value

4 Argument passed and a return value

Syntax:

function declaration:

int function ( int );
function call: function ( a );
function definition:
int function( int a )
{
statements;
return a;
}

Example

#include <stdio.h>
int add(int, int);
int add(int x, int y) 
{
int sum = x+y;
return(sum);
}
int main()
{
int sum = add(23, 31);
printf("%d", sum);
return 0;
}

Now let us quickly look at the C library functions which are important in order to write a program.

C Library Functions

Library functions are functions in C which are pre-defined and are present by default. You just have to include the specific header file in the program & you can use the functions defined in that header file. Each header file provides specific kind of functionality. The extension of the header file is .h.

For example, to use the printf/scanf functions we need to include stdio.h in our program, which provide functionality regarding standard input/output.

Following is the list of header files.

1stdio.hStandard input/output header file
2conio.hConsole input/output header file
3string.hString related library functions such as gets(), puts(),etc.
4stdlib.hGeneral library functions such as malloc(), calloc(), exit(), etc.
5math.hMath operations related functions such as sqrt(), pow(), etc.
6time.hTime-related functions
7ctype.hCharacter handling functions
8stdarg.hVariable argument functions
9signal.hSignal handling functions
10setjmp.hJump functions
11locale.hLocale functions
12errno.hError handling functions
13assert.hDiagnostics functions

Now after going through the above C functions you would have understood each & every nuance of function & how to implement it in C language. I hope this blog is informative and added value to you.

Thus we have come to an end of this article on ‘Functions In C’. If you wish to learn more, check out the Java Training by Edureka, a trusted online learning company. Edureka’s Java J2EE and SOA training and certification course is designed to train you for both core and advanced Java concepts along with various Java frameworks like Hibernate & Spring.

Got a question for us? Please mention it in the comments section of this blog and we will get back to you as soon as possible.

Comments
0 Comments

Join the discussion

Browse Categories

Subscribe to our Newsletter, and get personalized recommendations.