Skip to main content

C - Functions:

A function is a block of code that performs a specific task.

The function contains the set of programming statements enclosed by {}.

 

Advantage of functions in C:

There are the following advantages of C functions.

  • By using functions, we can avoid rewriting same logic/code again and again in a program.
  • We can call C functions any number of times in a program and from any place in a program.
  • We can track a large C program easily when it is divided into multiple functions.
  • Reusability is the main achievement of C functions.
  • However, Function calling is always a overhead in a C program.

 

Types of function:

There are two types of function in C programming:

  • Standard Library Functions / Predefined functions.
  • User Defined Functions.

Standard Library  Functions/ Predefined Functions:

Standard library functions are also known as built-in functions. Functions such as puts()gets()printf()scanf() etc are standard library functions. These functions are already defined in header files (files with .h extensions are called header files such as stdio.h), so we just call them whenever there is a need to use them.

For exampleprintf() function is defined in <stdio.h> header file so in order to use the printf() function, we need to include the <stdio.h> header file in our program using #include <stdio.h>.

User Defined Functions:

The functions that we create in a program are known as user defined functions or in other words you can say that a function created by user is known as user defined function.

 Syntax:

return_type function_name(datatype param1,datatype param2,....datatype paramN)
{

    //Block of Code
    return value;
}

  • return Type − A function may return a value. The return_type is the data type of the value the function returns. Some functions perform the desired operations without returning a value. In this case, the return_type is the keyword void.

  • Function Name − This is the actual name of the function. The function name and the parameter list together constitute the function signature.

  • Parameters − A parameter is like a placeholder. When a function is invoked, you pass a value to the parameter. This value is referred to as actual parameter or argument. The parameter list refers to the type, order, and number of the parameters of a function. Parameters are optional; that is, a function may contain no parameters.

  • Function Body − The function body contains a collection of statements that define what the function does.

Example:

int sum(int a,int b)
{
	int c=a+b;
    return c;
}

Different aspects of function calling

A function may or may not accept any argument. It may or may not return any value. Based on these facts, There are four different aspects of function calls.

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

Function without arguments and without return vlaue:

Syntax:

void function_name()
{
	function body
   
}

Sample Program:

#include< stdio.h >
int main()
{
	additon();
    
    return 0;
}

void addition()
{
	int a=10,b=20;
    print("sum=%d",(a+b));
}

Function without arguments and with return vlaue:

Syntax:

return_type function_name()
{
	function body
   return value
}
Sample Program:

#include< stdio.h >
int main()
{
	int result;
	result=additon();
    printf("the sum is:%d",result);
    
    return 0;
}

int addition()
{
	int a=10,b=20;
    return (a+b);
}

Function with arguments and without return vlaue:

Syntax:

void function_name(datatype param1,datatype param2...datatype paramN)
{
	function body
 
}
Sample Program:

#include< stdio.h >
int main()
{
	int a=10,b=20;
    addtion(a,b)
	
    return 0;
}

void addition(int a,int b)
{
	printf("the sum is:%d",(a+b));
}

Function with arguments and with return value:

Syntax:

int function_name(datatype param1,datatype param2...datatype paramN)
{
	function body
 	return value;
}
Sample Program:

#include< stdio.h >
int main()
{
    int a=10,b=20,result;
    result = addtion(a,b);
    printf("the sum is:%d",result);
    return 0;
}

int addition(int a,int b)
{
	return (a+b);
}

Comments