Register Now

Login

Lost Password

Enter your email to reset your password.

BY Author

C Functions

Generally, a program is divided into parts to avoid complexity.
Each part is used to perform a specific task. These parts are nothing but functions.

For better understanding, we can consider an example of a house.
C Programming Language Real Life Example Similar to Function

For us, the house is a single entity. But it is divided into many parts as rooms according to the activities (function) that we perform in those rooms.
For example, Kitchen room is used for cooking food; Bedroom is used for sleeping, Living room for general purpose activities, Bathroom & Toilet for cleaning activities.
Imagine what would happen if all the activities are done in a single big room. Oh! It’s very tough to imagine.

Similarly in C programming, writing a large program as a single part makes a program more complex.
To avoid complexity, it became necessary to divide a large program into different. This is the reason which brought function concept into existence in C programming.

So in a programmatically view, house acts as a complete single program and rooms as its different parts (functions).
Depending on the type of the activity that we want to perform we choose the room (function/part) of the house (program).
Generally, Living room acts as an entrance to a house. Similarly main() function acts as an entrance for program control to access other functions directly or indirectly.

Therefore simply we can say that the function is a part of the program that performs a specific task.

Syntax for function

returnType functionName(parameter_list)
{
    // programming
    // statements
}

A function is divided into two parts, known as a function header & a function body, as shown below:

C Programming Language Syntax of Function

returnType ( Return Type)

A return type of the function is decided on the type of the value that should be returned by the function.
For example, if a function returns an integer value, its return type should be int. If a function returns double value, its return type should be double.
If a function does not return any value, its return type must be void.

functionName (Function Name)

It can be any name.
But it is better to name the function that is suitable to the task performed by the function.

parameter_list (Parameter List)

It is the list of parameters arranged in sequence with suitable type.
Parameters are used to receive value required for functions operation.
The function can have any number of parameters.
Usually, if there is no need to receive values for a function, the parameter list is kept empty, or it is better to write keyword void inside the parenthesis.

Function body
It is the part of the function where the actual task is performed.
It contains the statements required for operations.

Example of functions

Here we have created two functions performing two distinct tasks.
Function with name kitchen for performing cooking related task & function with name bedroom for sleeping purpose.

void kitchen()
{
    printf("I entered in a Kitchen room");
    printf("I cooked the food");
    printf("I ate food\n");
}

                                                                               And

void bedroom()
{
    printf("I entered in a Bedroom");
    printf("I slept");
    printf("I woke up in thing\n");
}

Note

1. C program can contain any numbers of functions.
 2. If a C program contains single function, then it must be the main() function.

Advantages of functions

Makes program simple

It helps the program to divide into different parts instead of writing whole code into one part. It results in avoiding the complexity of a program.
Becomes easy to understand, maintain & debug.

Code reusability

The Same function can be used several times at different places. For that, we just need to call the function whenever required.

Function Calling

C Programming Language Function Calling

To execute any certain function in a program, the programmer needs to invoke/make a call to that function using function calling code.

General form of function call

function_Name(argument_list);

The argument list is kept empty if called function does not receive any value.

Example of function calls without argument

bathroom();

This code is used to call the bathroom function in a program.

kitchen();

This code is used to call the kitchen function in a program.

bedroom();

This code is used to call the bedroom function in a program.

Note

A function that calls another function is known as caller function.
 A function that is called by another function is known as called function.

Following is the example to explain function working & its calling

#include<stdio.h>
void kitchen();
void bedroom();
void bathroom();

int main()   // caller function
{
    printf("\n I entered in a Living room\n\n");
	
    bathroom(); // call to bathroom() function
    kitchen();  // call to kitchen() function
    bedroom();  // call to bedroom() function
	
    return 0;
}

void kitchen()  // called function
{
    printf("\n I entered in a Kitchen room");
    printf("\n I cooked a food");
    printf("\n I ate food\n\n");
}

void bedroom()  // called function
{
    printf("\n I entered in a Bedroom");
    printf("\n I slept");
    printf("\n I woke up in the morning\n\n");
}

void bathroom()  // called function
{
    printf("\n I entered in a Bathroom");
    printf("\n I got refreshed\n\n");
}

Output

I entered in a Living room

I entered in a Bathroom
I got refreshed

I entered in a Kitchen room
I cooked a food
I ate food

I entered in a Bedroom
I slept
I woke up in the morning

For the better understanding of the above program look at the diagram shown below:

C Programming Language Function Call Working Example

Small example showing code reusability

Here the same function bedroom() is used/called again & again.

#include<stdio.h>
void bedroom();

int main()
{
    printf("\n I entered in a Living room\n");
	
    bedroom(); // call to bedroom function
    bedroom(); // call to bedroom function
    bedroom(); // call to bedroom function
	
    return 0;
}

void bedroom()
{
    printf("\n I entered in a Bedroom");
    printf("\n I slept");
    printf("\n I woke up in the morning\n");
}

Output

I entered in a Living room

I entered in a Bedroom
I slept
I woke up in the morning

I entered in a Bedroom
I slept
I woke up in the morning

I entered in a Bedroom
I slept
I woke up in the morning

 

Leave a reply