How to write a function in C?

Kailash Chandra Behera | Thursday, November 19, 2020

Introduction

A function is a self-defined block of statements that are used for the readability of the same block of statements whenever a function is called. Here in this article, we will discuss the step by step process to write a function in c.

Getting Started

A function is a block of code that performs some operation. A function can optionally define input parameters that enable callers to pass arguments into the function. A function can optionally return a value as output.

Functions are useful for encapsulating common operations in a single reusable block, ideally with a name that clearly describes what the function does. The following function accepts two integers from a caller and returns their sum; a and b are parameters of type int.

There is no practical limit to function length, but good design aims for functions that perform a single well-defined task. Complex algorithms should be broken up into easy-to-understand simpler functions whenever possible.

Functions that are defined at class scope are called member functions. In C++, unlike other languages, a function can also be defined at namespace scope (including the implicit global namespace). Such functions are called free functions or non-member functions; they are used extensively in the Standard Library.

Functions may be overloaded, which means different versions of a function may share the same name if they differ by the number and/or type of formal parameters. For more information, see Function Overloading.

Parts of function in C

A c program function has five parts, the following are the five parts of the function in c.

Function Prototype

It is the image of the function. It consists of three parts that are given below.

  1. Function return type.
  2. Function name.
  3. Function argument types.

 int sum(int a) /* Function prototype */  

Function Definition/ Declaration.

It contains the block of codes that has to be executed whenever a function is called. The function contains the formal arguments which are executed when the function is called.

 int factorial( int num )   /* Function definition */  
 {  
   statements;  
 }  

A function declaration followed by a semicolon may appear in multiple places in a program. It must appear prior to any calls to that function in each translation unit. The function definition must appear only once in the program, according to the One Definition Rule (ODR).

Function Call

The function call contains the actual arguments which are passed to the function definition section. If the function is a void function, then after execution of the definition section. It doesn’t return values to the function call but if it is a non-void type function, then it returns a value to the function call.

Function argument

The function can be invoked, or called, from any number of places in the program. The values that are passed to the function are the arguments, whose types must be compatible with the parameter types in the function definition. There are two types of function argument.

  1. Formal argument:- A formal argument contains a copy of the actual argument. By using these values functions are executed and return value to the function call.
  2. Actual argument:- Actual arguments contain the original values which are passed to the function definition for execution.

Return Statement

The return statements are declared based on the function return type. If the function is void type, then no return statement is needed. So the output statement must be declared in the function definition section whereas, if the function is a non-void type function then the return statement must be declared.

After the execution of the function definition section, the return statement returns the value to the function call. But if the main() is a non-void type then the return statement returns an integer value to the operating system.

Example of function in C

Function with no argument and no return value

 void function()  
            {  
             statements;  
            }  

Function with argument and no return value

 void function(int a)   
       {   
        statements;   
       }   

Function with argument and return value

 int sum(int a, int b)  
 {  
   return a + b;  
 }  

Function with no argument and return value

 int function()   
       {   
        return 1;   
       }   

Summary

Here in the above, we learnd what is function in c, defination of function, how to declare function and parts of function in c. I hope you have enjoyed it a lot.

Thanks


No comments: