User Defined Functions

Function: A function in C is a modular unit of code that performs a specific task or set of tasks. It is a (self-contained) block of code that can take input parameters, perform operations, and return output values. Functions can be called from anywhere within a program and can be reused to perform the same task in different parts of the program.

Advantages of Function:

  1. Function increases code reusability.

  2. The length of the source program can be reduced by using functions at appropriate places.

  3. The program development will be faster.

  4. The program debugging will be easier.

  5. Large number of programmers can be involved.

  6. The program can be developed in a short period of time.

  7. The program can be developed in different places.

  8. The program can be tested and compiled independently by different members of a programming team.


 A function has 3 elements.

  1. Function Declaration (Function Prototype)

  2. Function Call

  3. Function Definition

 a) Function Declaration (Function Prototype)

A function declaration also known as Function Prototype consists of four parts:

  • Function type(return type)

  • Function name

  • Parameter list

  • Terminating Semicolon

Syntax:

Function_type Function_name(Formal Parameter list);

Example:

int sum(int a, int b);

or

int sum(int, int);

Points to note:

  1. The parameter list must be separated by commas.

  2. The parameter names do not need to be the same in the prototype declaration and the function definition.

  3. The types must match the types of parameters in the function definition, in number and order.

  4. Use of parameter names in the declaration is optional.

 

Function Parameter:

The parameters used in function declaration and function definition are called formal parameters and those used in function call are called actual  parameters.

The formal and actual parameters must match exactly in type, order and number. Their names, however, do not need to match.

 

Examples: sum=add (a, b, c); // here a, b, c are called actual parameters.

         int add(int x , int y, int z); // here x, y, z are formal parameters.

 

Function Return: Function can be organized into 2 types.

a) Function that has not a return value i.e. void function.

b) Function that has a returned value i.e. int function.

 

b) Function Call

A function can be called by simply using the function name followed by a list of actual parameters, if any, enclosed in parenthesis.

Static variables are created and initialized once, on the first call to the function. Subsequent calls to the function do not recreate or re-initialize the static variable.

Example:

c=sum (a,b);

3. Function Definition

The function definition is an independent program module that is specially written to implement the requirement of the function.


A function definition, also known as function implementation shall include the following elements.

  1. Function type

  2. Function Name

  3. List of parameters

  4. Local variable declarations

  5. Function statements

  6. A return statement

 

All the six elements are grouped into two parts, namely,

  • Function Header(First three elements)

  • Function Body ( Second three elements)

Syntax:

Function_type Function_Name(Formal Parameter list)

{

Local variable declaration;

Executable statement 1;

Executable statement 2;

………………….

………………….

return statement;

}

Note that a semicolon is not used at the end of the Function Header of Function definition.



Netra Koirala

Netra Koirala

Computer Science Educator

Passionate computer science educator and author. Provides free study notes, practical guides, and tutorials for Class 9, 10, 11, 12, and B.Sc CSIT students in Nepal. Years of teaching experience in computer science fundamentals.

Computer Science notes, tutorials, MCQs, and educational resources for Nepal students. Covering Class 9, SEE preparation, Class 11, Class 12, SLC, programming, DBMS, networking, HTML, JavaScript, PHP, OOP and more.

Featured Post

Grade 10 Computer Science: Specification Grid & Model Questions

Specification Grid & Model Questions of Computer Science | Grade 10 📚 Examination Resource Specification Grid & M...

Followers