Functions

A function is a group of statements that together perform a task. Every C program has at least onefunction, which is main(), and all the most trivial programs can define additional functions. You can divide up your code into separate functions.

c_programming_working_of_functions

Why Using Function?

  • C functions are used to avoid rewriting same logic/code again and again in a program.

  • There is no limit in calling C functions to make use of same functionality wherever required.

  • We can call functions any number of times in a program and from any place in a program.

  • A large C program can easily be tracked when it is divided into functions.

  • The core concept of C functions are, re-usability, dividing a big task into small pieces to achieve the functionality and to improve understandability of very large C programs.

Advantages in Function

  • Code Re-usability

  • Develop an application in module format.

  • Easily to debug the program.

  • Code optimization: No need to write lot of code.

Function Declaration

A function declaration is the process of tells the compiler about a function name. The actual body of the function can be defined separately.

Syntax

return_type function_name(parameter);

Note

At the time of function declaration function must be terminated with ;.

Function Defination

Defining of function is nothing but give body of function that means write logic inside function body.

Syntax

return_type function_name(parameter)

{

function body;

}

Note

  • 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.

Calling a Function

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.

Syntax

function_name();

 or

variable=function_name(argument);

Note

At the time of function calling function must be terminated with ‘;’.

Example For Function

simple-function-program

Output

simple-function-output

Passing Parameters To The Function

They are two types of parameters :

1.call by valu.

2.call by reference

1.Call by value

In call by value, original value can not be changed or modified. In call by value, when you passed value to the function it is locally stored by the function parameter in stack memory location. If you change the value of function parameter, it is changed for the current function only but it not change the value of variable inside the caller method such as main().

call-by-value-program

Output

call-by-value-output

2.call by reference

In call by reference, original value is changed or modified because we pass reference (address). Here, address of the value is passed in the function, so actual and formal arguments shares the same address space. Hence, any value changed inside the function, is reflected inside as well as outside the function.

call-by-reference-program

call-by-reference-output

Return Statement

The return statement is used to terminate the execution of a function and returns control to the calling function. 

when the return statement is encountered,the program execution resumes in the calling function at the point immediately following the function call.

Program On return statement

function-program

Output

simple-function-output

Recursive Function

When Function is call within same function is called Recursion. The function which call same function is called recursive function. In other word when a function call itself then that function is called Recursive function.

Recursive function are very useful to solve many mathematical problems like to calculate factorial of a number, generating Fibonacci series, etc.

screenshot_8

Advantages of recursion

 

  • Function calling related information will be maintained by recursion.

  • Stack evaluation will be take place by using recursion.

  • In fix prefix, post-fix notation will be evaluated by using recursion.

 

Disadvantages of Recursion

  • It is a very slow process due to stack overlapping.

  • Recursive programs can create stack overflow.

  • Recursive functions can create as loops.

Find the Factorial of any number using recursion

factorial-recursion

Output

factorial-recursion-output

Find the Table of any number using recursion

table-recursion

Output

table-recursion-output