Introduction to Functions in C Programming

aimtocode

Function Definitions

A function is a self-contained program segment that carries out some specific, well-defined task. The function contains the set of programming statements enclosed by {}.

Functions break large computing program into smaller ones. C has been designed to make functions efficent and easy to use.

C programs generally consist of many small functions rather than a few big ones.

The use of functions avoids redundant programming of the same instructions can be placed within a single function, which can then accesses whenver it is needed.

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.

Moreover, a different set of daa can be transferred to the function each time it is accesses. C supports the user of library functions, which are used to carry out a number of commonly used operations or calculations.

Printf() and scanf() are examples of library functions. C also allows programmers to write their own functions for carrying out various individual tasks.

Advantage of C Functions

  • By using functions, we can avoid rewriting same logic/code again and again in a same program.
  • We can call C functions any number of times in a program and from any place in a program.
  • We can track a large C program easily when it is divided into multiple functions.
  • Reusability is the main achievement of C functions.
  • However, Function calling is always a overhead in a C program.

Function Aspects

1.Function Declaration: A program is a set of definitions of variables and functions. Communications between the functions is through arguments and values returned by the functions.

A function declaration tells the compiler about a function's name, return type, and parameters. A function definition provides the actual body of the function.

2.Function call: Function can be called from anywhere in the program. The parameter list must not differ in function calling and function declaration. We must pass the same number of functions as it is declared in the function declaration.

3.Function Definition It contains the actual statements which are to be executed. It is the most important aspect to which the control comes when the function is called. Here, we must notice that only one value can be returned from the function.

 return_type function_name( parameter list ) {
   body of the function
 }

Syntax:

SNC function aspectsSyntax
1Function declarationreturn_type function_name (argument list);
2Function callfunction_name (argument_list)
3Function definitionreturn_type function_name (argument list) {function body;}


Function Declaration

A program is a set of definitions of variables and functions. Communications between the functions is through arguments and values returned by the functions.

Example 1:



Output:

 
 Hello, Welcome to aimtocode

Example 2:



Output:

 Enter some number for finding square
 2
 Square of the given number 2.000000 is 4.000000

Function with no argument and no value



Output:

  Hello aimtocode

Function with no argument and with return value



Output:


 Calculate the sum of two numbers:

 Enter two numbers 10 
 24 

 The sum is 34


Function with argument and without return value



Output:

 Calculate the sum of two numbers:
 Enter two numbers:10
 20 
 The sum is : 30 

Library Functions

SNHeader fileDescription
1stdio.hThis is a standard input/output header file. It contains all the library functions regarding standard input/output.
2conio.hThis is a console input/output header file.
3string.hIt contains all string related library functions like gets(), puts(),etc.
4stdlib.hThis header file contains all the general library functions like malloc(), calloc(), exit(), etc.
5math.hThis header file contains all the math operations related functions like sqrt(), pow(), etc.
6time.hThis header file contains all the time-related functions.
7ctype.hThis header file contains all character handling functions.
8stdarg.hVariable argument functions are defined in this header file.
9signal.hAll the signal handling functions are defined in this header file.
10setjmp.hThis file contains all the jump functions.
11locale.hThis file contains locale functions.
12errno.hThis file contains error handling functions.
13assert.hThis file contains diagnostics functions.
aimtocode