x

C – Function

Prev     Next

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. You can learn below concepts of C functions in this section in detail.


  1. What is C function?
  2. Uses of C functions
  3. C function declaration, function call and definition with example program
  4. How to call C functions in a program?
    1. Call by value
    2. Call by reference
  5. C function arguments and return values
    1. C function with arguments and with return value
    2. C function with arguments and without return value
    3. C function without arguments and without return value
    4. C function without arguments and with return value
  6. Types of C functions
    1. Library functions in C
    2. User defined functions in C
      1. Creating/Adding user defined function in C library
  7. Command line arguments in C
  8. Variable length arguments in C

1. What is C function?

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.

2. Uses of C functions:

  • C functions are used to avoid rewriting same logic/code again and again in a program.
  • There is no limit in calling C functions to make use of same functionality wherever required.
  • We can call functions any number of times in a program and from any place in a program.
  • A large C program can easily be tracked when it is divided into functions.
  • The core concept of C functions are, re-usability, dividing a big task into small pieces to achieve the functionality and to improve understandability of very large C programs.

3. 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 functions aspects
syntax
function definition Return_type function_name (arguments list)
{ Body of function; }
function call function_name (arguments list);
function declaration return_type function_name (argument list);

Simple example program for C function:

  • As you know, functions should be declared and defined before calling in a C program.
  • In the below program, function “square” is called from main function.
  • The value of “m” is passed as argument to the function “square”. This value is multiplied by itself in this function and multiplied value “p” is returned to main function from function “square”.

Output:

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

4. 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

1. Call by value:

  • In call by value method, the value of the variable is passed to the function as parameter.
  • The value of the actual parameter can not be modified by formal parameter.
  • Different Memory is allocated for both actual and formal parameters. Because, value of actual parameter is copied to formal parameter.

Note:

  • Actual parameter – This is the argument which is used in function call.
  • Formal parameter – This is the argument which is used in function definition

Example program for C function (using call by value):

  • In this program, the values of the variables “m” and “n” are passed to the function “swap”.
  • These values are copied to formal parameters “a” and “b” in swap function and used.

Output:

values before swap m = 22
and n = 44
values after swap m = 44
and n = 22

2. Call by reference:

  • In call by reference method, the address of the variable is passed to the function as parameter.
  • The value of the actual parameter can be modified by formal parameter.
  • Same memory is used for both actual and formal parameters since only address is used by both parameters.

Example program for C function (using call by reference):

  • In this program, the address of the variables “m” and “n” are passed to the function “swap”.
  • These values are not copied to formal parameters “a” and “b” in swap function.
  • Because, they are just holding the address of those variables.
  • This address is used to access and change the values of the variables.

Output:

values before swap m = 22 
and n = 44
values after swap a = 44
and b = 22

Continue on C – function arguments and return values….

Continue on C – Library functions….

Continue on C – User defined functions & adding them in C library….

Continue on C – Command line arguments….

Continue on C – Variable length arguments….

Prev     Next



Like it? Please Spread the word!