C enables the programmer to break up a program into segments commonly known as functions, each of which can be written more or less independently of the others.

Every function in the program is supposed to perform a well defined task. Therefore, the program code of one function is completely insulated from the other functions.

Every function interface to the outside world in terms of how information is transferred to it and how results generated by the function are transmitted back from it.

Functions in c programming Language for beginnersFrom the figure we can see that the main() calls the function named as funcion1(). Therefore, main() is known as the calling function and the function or method which is called from main() that is function1() is known as called function.

The moment the compiler encounters a function call, instead of executing the next statement in the calling function, the control jumps to the statements that are the part of the called function. After the called function is executed, the control is returned back to the calling function, in above case, control is returned back to main().

It is not necessary that the main() function can call only one function; it can call as many function as the programmer wants it to call and as many times as it wants.

NOTE: Another point is that it is not only the main() function that can       call other functions. A function can call any other function.

Functions in c programming Language for beginnersNEED FOR FUNCTION:

Let us analyze the reason for segmenting a program into manageable chunks as it is an important aspect to the programming.

  • Dividing the program into separated well defined functions. This simplifies the process of getting the total program to work.
  • This approach is referred to top-down approach.
  • Understanding coding, and testing multiple functions are far easier than doing the same for a single huge program.
  • It is easy to develop a large program by developing the smaller program.
  • It is easy and well maintained method to design a large program or software; here multiple programmers can work on a single program at the very same time.

USING FUNCTIONS:

When we execute the C program, the operating system calls the main() function of the program which marks the entry point of the execution. When the program is well executed, main() returns some value to the operating system.

While using the function we will be using the following terminologies:

  • A function f() that used another function g(), is known as the calling function and the g() is known as the called function.
  • The input that a function takes is known as the arguments/ parameters.
  • When a called function returns some values to the calling function it is known as return the result.
  • The calling function may or may not pass the parameters to the called function. It is called function that accepts the arguments, the calling function will pass parameters, and else it will not do so.
  • Function declaration is a declaration statement that indentifies a function with its name, a list of arguments that it accepts, and the type of data it return.
  • Function definition consists of a function header that identifies the function, followed by the body of the function containing the executable code for that function.

FUNCTION DECLARATION/ FUNCTION PROTOTYPE:

Before using the function, the compiler must know about the number and type of parameters that the function expects to receive and data type of the value that it will return to the calling function or program.

The general format for declaring a function that accepts some arguments and returns some value as a result can be given as:

Here the function_name is a valid name for the functions.

Naming a function follows the same rules as naming variables. The function name is used to call it for execution of a program. Every function must have a different name that indicates a particular job that the function does.

Return_data_type specifies the data type of the value that will be returned to the calling function as a result of processing performed by the called function.

data_type var1 , data_type var2 are the list of arguments. These variables are passed from the calling function to called function.

FUNCTION DEFINITION:

When a function is defined, space is allocated for that function in the memory. A function definition comprises of two things:

  1. Function header
  2. Function body

The syntax of a function definition can be given as:

The number and the order of the arguments in the header must be same as that given in the function declaration statement.

While return_data_type function_name (data_type var1, data_type var2) is known as function header, the rest of the portion comprising program statements within { } is the function body which contains the code to perform the specific task.

FUNCTION CALL:

The function call statement invokes the function. When a function is invoked the compiler jumps to the called function to execute the statements that are the part of that function. Once the called function is executed, the program control passes back to the calling function.

The function call statement has the following syntax:

 

When the function declaration is present before the function call, the compiler can check if the correct number and type of arguments are used in the function call and the returned value, if any, is being used reasonably.

NOTE:

The list of arguments used in the function call is known as the actual arguments.