Functions in C

Ratings:
(4)
Views:387
Banner-Img
  • Share this blog:

Functions in C

Self containing block of one or more statements or a sub-program which is designed for a particular task is called functions. A function is a group of statements that together perform a task. Every C program has at least one function, which is main(), and all the most trivial programs can define additional functions.

A large C program is divided into basic building blocks called C function. C function contains set of instructions enclosed by “{  }” which performs specific operation in a C program. Actually, Collection of these functions creates a C program.

C functions are basic building blocks in a program. All C programs are written using functions to improve re-usability, understandability and to keep track on them.

syntax :

returntype    function name (parameters)

{

statement block;

return statement;

}

- Return type, parameters and return statements are optional

- In implementation , whenever a function is returning the value then specify the return statement is optional . In this case, we will get a warning message called “ function should return a value”

- Whenever the function is returning the value, then collecting the value also optional

In ‘c’ programming language, function are classified into two types.

1. Library functions or Built in functions or predefined functions

Predefined functions in C are those that are part of the C library. These are present in any of the various headers that can be included in a program.

2. User defined functions

User defined function has to be developed by the user at the time of writing a program.

Functions have a basic structure. Their format is:

return_ data_type function name (arguments, arguments)
    data _type _declarations of arguments;
    {
       function body
    }

C FUNCTION DECLARATION, FUNCTION CALL AND FUNCTION DEFINITION:

There are 3 aspects in each C function. They are,

  • Function declaration or prototype  – This informs compiler about the function name, function parameters and  return value’s data type.
  • Function call – This calls the actual function
  • Function definition – This contains all the statements to be executed.

c function

Example Of a Function

#include<stdio.h>
#include<conio.h>
void add(int x, int y)
{
     int result;
     result = x+y;
     printf("Sum of %d and %d is %d ", x,y,result);
}
void main()
{
     clrscr();
     add(10,20);
     add(30,40);
     add(50,60);
     getch();
}

OUTPUT:

Sum of 10 and 20 is 30

Sum of 30 and 40 is 70

Sum of 50 and 60 is 110

Types of Functions In C

There are 4 types of Functions in C

1. Functions with no arguments and no return values.

2. Functions with arguments and no return values.

3. Functions with arguments and return values.

4. Functions with no arguments and return values.

HOW TO CALL C FUNCTIONS IN A PROGRAM?

There are two ways that a C function can be called from a program. They are,
  1. Call by value
  2. Call by reference

Parameters in C

A Parameter is the symbolic name for "data" that goes into a function. There are two ways to pass parameters in C: Pass by Value, Pass by Reference.

Pass by Value

Pass by Value, means that a copy of the data is made and stored by way of the name of the parameter. Any changes to the parameter have NO affect on data in the calling function.

Pass by Reference

A reference parameter "refers" to the original data in the calling function. Thus any changes made to the parameter are also made to the original variable.

Advantages of Functions

- By using function, we can avoid rewriting the code in the applications

- By using functions, we can develop an application in module format

- Program development will become easy by using function

- when we are developing an application in module format, then we can easily trace and debug the applications.

- By using the functions , we can keep track the logic implementation of the program.

- The basic purpose of the function is : code reuse

- A ‘C’ program is a combination of functions

- From any function, we can invoke any other function

- Always compilation process will take place from top to bottom

- Execution process will starts with main and ends with main only.

- In implementation, when we are calling a function, which is defined later, for avoiding the compilation error we   require to go for forward declaration i.e prototype is required

- Declaration of function means, require to specify return type, name of the function and parameter type information

- In function definition first line is called function declarator or function header

- Always a function is not returning any values then specify the return type as void

  void means “nothing” ie. no return value.

- In implementation, whenever a function is returning the value then specify the return type as return value type. ie. what type of value we are returning same type of return statement is required.

- Default return type of the function is an int, default parameter type is void

Our course design of tutorials is practical and informative. At TekSlate, we offer resources to help you learn various IT courses. We avail both written material and demo video tutorials. For in-depth knowledge and practical experience explore Online C Tutorials.

You liked the article?

Like : 0

Vote for difficulty

Current difficulty (Avg): Medium

Recommended Courses

1/15

About Author
Authorlogo
Name
TekSlate
Author Bio

TekSlate is the best online training provider in delivering world-class IT skills to individuals and corporates from all parts of the globe. We are proven experts in accumulating every need of an IT skills upgrade aspirant and have delivered excellent services. We aim to bring you all the essentials to learn and master new technologies in the market with our articles, blogs, and videos. Build your career success with us, enhancing most in-demand skills in the market.


Stay Updated


Get stories of change makers and innovators from the startup ecosystem in your inbox