Functions in C with Examples.

Functions in C with Examples.

To make larger programs manageable, programmers use functions. Functions means modularizing larger programs into sub programs. We create these functions to perform a particular task whenever necessary. Compiling and testing these programs separately is possible. Also we can use them in other programs.

A task is divided into sub tasks which help us to reduce the complexity of a main program. If a user wants a particular job to be done again then the user can call the defined functions. Because of this the repetition of the code is reduced.

It is easy to write and debug the code since we divide the main logic of a program into sub tasks.

Types of Functions

1. Library/Built-in/System defined Functions
2. User Defined Functions

1. Library/Built-in/System defined Functions

In every programming language there are some library functions that developers define at the time of development. These are stored in the system itself. In C/C++, for using these files we have to include respective header files. For e.g. To make use of readymade pow(int,int) function we have to use #include<math.h> header file.

2. User Defined Functions

These are the functions programmer write as per their requirements. Thus the functions allows us to create a logical grouping of the code.

Syntax

return_data_type name_of_the_function(parameter_list)
{
    local varibales;
    .
    .
    .
    .
    body of function;
    .
    .
    .
    .
    return statement;
}

Steps to create a Function

There are three steps to create a function. They are –
1. Declaration/Signature/Prototype of a Function
2. Calling Function
3. Definition of a Function

1. Declaration/Signature/Prototype of a Function.

Function declaration is also called function prototype in C. Function prototype assign return data type, name of the function and parameter list (if necessary). We can declare a function after or before we declare main(). Furhter we will se function prototype in C with examples.

2. Calling Function.

Generally, we call a function in main(). But we can also can function in other functions. Unless we call a function, it won’t execute.

3. Definition of a Function.

In this part we specify the task that a function has to perform.

Ways to call a Function

There are two ways of calling a function. They are –
1. Call by value
2. Call by reference

1. Call by value.

In this technique the original values of the arguments are kept aside and the duplicate values are given to the calling function. Hence if in execution of function there is loss or damage to the variables, it does not affect the original values. Only disadvantage of using call by value is that it creates duplicate locations to store duplicate values. This affects the speed of program execution.

2. Call by reference.

This method copies the address of the actual parameters and assign it to the formal parameters. Due to this, if we make any changes to the formal parameters the value of actual parameters also changes. For this method we use two special operators.
1. Reference operator (&).
2. Dereference operator (*).

Now let’s see an example of function.

E.g. Write a program in C to show use of function. using
1. Call by value.
2. Call by reference.

//Call by value method

#include<stdio.h>
void display(int, int);  //Declaring a function
int main()
{
    int a, b;
    printf("\n Enter two nos:");
    scanf("%d%d", &a, &b);
    display(a,b);    //Calling a function using call by value method //a and b are actual parameters
    return 0;
}


void display(int a, int b)   //Definition of a function // a and b are formal parameters
{
    int sum;
    sum = a + b;
    printf("\n Sum is :%d", sum);
}

Output :-

Function prototype in C with examples
//Call by value reference
#include<stdio.h>
void display(int *, int *);  //Declaring a function
int main()
{
    int a, b;
    printf("\n Enter two nos:");
    scanf("%d%d", &a, &b);
    display(&a,&b);    //Calling a function using call by reference method //a and b are actual parameters
    return 0;
}

void display(int *a, int *b)   //Definition of a function // a and b are formal parameters
{
    int sum;
    sum = *a + *b;
    printf("\n Sum is :%d", sum);
}

Output :-

Function prototype in C with examples

Explanation :-

The explanation for both the programs is almost same. In both the programs header files are checked first. If appropiate header files are present then the compiler checks the next line. Here we declare our functions. It is also known as prototype or signature. In call by value we only mention type of parameter i.e. int if integer data. In call by reference we use ‘*’ symbol as reference operator since we assign address of actual parameters to formal parameters.

Then the compiler enters main() function. Here we declare two variables of integer data type. We ask user to enter two values which are assigned to a and b. Then we call the function. In call by value we simply specify name of the function and parameters (variable names). But in call by reference we put ‘&’ operator to obtain address of the actual parameters.

In next step the compiler jumps to the definition of the function. Here the actual task of the function is mentioned. Our function simply calculates sum of two numbers. Thus here we declare a variable sum to store addition. And we print that value. After the task specified in functions are completed the compiler returns back to the main() function. And proceeds with the commands. If nothing is given then the program terminates.

Conclusion

That’s the concept of function prototype in C with examples.. We hope we have made you understand the concept very well. If you have doubt then just mail us at the mail id given in the contact us section. If you liked this post then do tell us in the comment section below. Do follow us on Instagram for daily updates.

For more such posts visit again!

ErrorFreeProgram.

4 COMMENTS

Leave a Reply

Your email address will not be published. Required fields are marked *