Function - C Tutorial

Function

A function is a group of statements that together perform a specific task. Every C program has at least one function, which is main().

A function can also be referred as a method or a sub-routine or a procedure, etc.

Why use function ?

Function are used for divide a large code into module, due to this we can easily debug and maintain the code. For example if we write a calculator programs at that time we can write every logic in a separate function (For addition sum(), for subtraction sub()). Any function can be called many times.

Advantage of Function

  • Code Re-usability
  • Develop an application in module format.
  • Easily to debug the program.
  • Reduces the size of the code, duplicate set of statements are replaced by function calls.
  • Code optimization : No need to write lot of code.

Types of functions

Predefined standard library functions

such as puts(), gets(), printf(), scanf() etc – These are the functions which already have a definition in header files (.h files like stdio.h), so we just call them whenever there is a need to use them.

User Defined functions

are the functions which are created by the C programmer, so that he/she can use it many times. It reduces complexity of a big program and optimizes the code.

C Function Definitions

A function definition specifies the name of the function, the types and number of parameters it expects to receive, and its return type. A function definition also includes a function body with the declarations of its local variables, and the statements that determine what the function does.

Syntax

return_type  function_name(parameter)
{
function body;
}

Return type: A function may return a value. The return_type is the data type of the value the function returns.Return type parameters and returns statement are optional.

Function name: Function name is the name of function it is decided by programmer or you.

Parameters: This is a value which is pass in function at the time of calling of function A parameter is like a placeholder. It is optional.

Function body: Function body is the collection of statements.

Function Prototypes or Function declaration

A function declaration precedes the function definition and specifies the name, return type, storage class, and other attributes of a function. To be a prototype, the function declaration must also establish types and identifiers for the function's arguments.

Syntax

    return_type  function_name(parameter);

Function Calls

When we call any function control goes to function body and execute entire code. For call any function just write name of function and if any parameter is required then pass parameter.

function_name(); 
    or  
variable=function_name(argument);

Function – Call by value method – In the call by value method the actual arguments are copied to the formal arguments, hence any operation performed by function on arguments doesn’t affect actual parameters.

Function – Call by reference method – Unlike call by value, in this method, address of actual arguments (or parameters) is passed to the formal parameters, which means any operation performed on formal parameters affects the value of actual parameters.

Recursive Functions

Any function in a C program can be called recursively; that is, it can call itself. The number of recursive calls is limited to the size of the stack.

Program To Calculate Factorial of a Number using Function

#include <stdio.h>
int factorial(int num);
int main()
{
	int num,fn;
	printf("Enter the number :");
	scanf("%d",&num);
	fn=factorial(num);
	printf("Factorial Number of %d is %d",num,fn);
	printf("\nEnter another number :");
	scanf("%d",&num);
	fn=factorial(num);
	printf("Factorial Number of %d is %d",num,fn);
    return 0;
}
int factorial(int num)
{
	int fn=1;
    while(num>=1)
    {
		fn=fn*num;
		num--;
    }
	return fn;
}

Output - Program To Calculate Factorial of a Number Using Function

Enter the number :4    
Factorial Number of 4 is 24
Enter another number :6    
Factorial Number of 6 is 720