C Functions

  • A function is a self-contained block of statements.
  • Function is the block of code which does predefined task.
  • Writing functions avoids rewriting the same code over and over.
  • Any C program at least contains one function.
  • C program is a collection of one or more functions.
  • This function is main(), this function is important because execution always start from main().
  • Without main(), C program does not execute.
  • The compiler always begins the program execution with main( ).
  • Every function in a program must be called directly or indirectly by main( ).
  • Any C program can contain any number of functions.
  • These function called or execute as there sequence given in main().
  • One function can call another function.
  • In a program, a function can be called any number of times.
  • It is not necessary, the order in which functions are defined and order in which functions are called will be same.
  • A function can call itself. Such a process is called recursion.
  • A function can not be defined in another function.

Advantages of function:

  • Plugability
  • Readability
  • Memory requirement
  • Debugging
  • Reusability
  • Modularity

Types of Function

There are basically two types of functions:

  1. predefined or library or build-in functions.
  2. user defined functions.

The predefined functions like printf(), scanf(),getchar() etc. These predefined functions are already defined in standard library files, like stdio.h, conio.h etc. Predefined functions are commonly used functions stored in a library.

The user-defined function is defined by a user. If a user wants to write function then user have to write the declaration of a function. There can be any number of user-defined functions. But it is necessary to have one predefined function i.e, main().

Function Elements

Function have following three elements

  1. Function calling
  2. Function declaration
  3. Function definition


Function calling:

Function calling means calling a function from anywhere in the program. A function is called whenever it is needed. A function is called by using their name. As given in above program add(a,b) is called as function calling. A function is called by using their name followed by a list of arguments enclosed in parenthesis and separated by commas.

In above program ‘add’ is a function name and ‘a’ and ‘b’ are called as actual arguments or actual parameters.
When program executes, main() function call add() function then compiler find out add() function in program.
If a function is present then compiler execute this function and return value to function calling if required.
In this way, a function can be call.

Function declaration:

Usually, the function declaration is written on top of the program. Function declaration does not have any body.
The function declaration is also called a function prototype. In above program add() takes two integer parameters and return integer value. Function declaration is ended with a semicolon ;.

Syntax for function declaration:

return_type – it represents datatype of the value that is return by function.
The return_type of function declaration and return_type of function definition must be same.
function_name – it is a name of a user-defined function.
datatype1 – this is the type of argument 1. The datatypes of the respected arguments have to be same as given in function definition.
datatype2 – this is the type of argument 2.
datatype n – this is the type of argument number n.

Function definition:

It is a block of code in which the role of the function is defined. As in above example, the actual logic of function add() is written in a function definition. Here ‘p’ and ‘q’ are called as formal parameters or formal arguments.Because they represent the name of the data item that is transferred into the function from the calling portion of the program. So the values stored in actual parameters are copied into formal parameters. Name of formal parameters and name of actual parameters can be same or different.
Even though their names are same then also they are different and occupied different memory location.

Syntax for function defination

The following condition must be satisfied for the function call.

  • The number of arguments in the function call and function declaration must be same.
  • The datatype of each of the argument in the function call should be same as the corresponding parameter in the function declaration statement.

Return statement:

  • The return statement immediately transfers the control back to the calling program.
  • It returns the value to calling program.
  • There is no restriction on the number of return statements in a function.
  • The return statement need not always be present at the end of the called function.
  • All the following are valid return statements.
  • In some programming situations, we want that a called function should not return any value. This is made possible by using the keyword void.
  • A function can return only one value at a time.
  • The following statements are invalid.

Functions Argument:

  • The list of variables used inside the parentheses of functions is called as arguments or parameters.
  • Any number of arguments can be passed to a function
  • If the value of a formal argument is changed in the called function, the corresponding change does not take place in the calling function.
  • The values stored in actual parameters are copied into formal parameters
  • NAme of formal parameters and name of actual parameters can be same or different.
  • Arguments can be passed to a function by two methods. They are,
    • call by value
    • call by reference
Help others by sharing the content!

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.