Functions
This note is about the Functions of C programming language. It explains the library function user-defined functions. It also explains the uses of function, function declaration, function call and arguments too.
Summary
This note is about the Functions of C programming language. It explains the library function user-defined functions. It also explains the uses of function, function declaration, function call and arguments too.
Things to Remember
- A number of statements grouped into a single logical unit are called as function. It is true that C program is collection of functions.
- The function main() in the program is executed first.
- It is necessary to have a single function main () in every C program along with other functions. It is observed that the main() function is also user defined function.
- The library functions are made available to the programmer in the inclusion of the software product. These are the functions determined by the users.
- The functions are used to enhance the program. The user defined functions help calculate the values and return them. The functions can be used in different programs as required.
- It is true that main is the main function without other functions cannot execute. It is the entry point.
- The function and its parameters are explicitly included within the function. Let us examine the function call as stated in above program.
- A function can be accessed or called by specifying its name followed by a list of arguments enclosed in the parentheses and separated by commas.
MCQs
No MCQs found.
Subjective Questions
No subjective questions found.
Videos
No videos found.

Functions
INTRODUCTION
A number of statements grouped into a single logical unit are called as function. It is true that C program is the collection of functions. The function main() in the program is executed first. The other functions are executed when the function main() calls them directly or indirectly.
It is necessary to have a single function main () in every C program along with other functions. It is observed that the main() function is also user defined function. It is executable with suitable parameters.
We are aware of syntax and use of commands, statements, and function in programming languages. C supports the use of library functions which are used to carry out a number of commonly used operations or calculations.
A C program can be modularized through the use of intelligence of such functions. Hence, it is a modular programming. The C language has two types of functions. They are:
- Built-functions or C library functions
- User-defined functions.
1. Built-function or C Library Functions:
The library functions are made available to the programmer in the inclusion of the software product. They are precisely constructed to determine the type of data they accept and the return type.
The library functions such as pow (), sqrt(), strcmp(), etc, are located in the Lib folder by default.
2. User-defined Functions:
These are the functions determined by the users. They are created for calculating different tasks. The general features and facilities obtained from the use of user-defined functions are:
- The repeated instructions can be placed within a single function which can then be accessed whenever it is needed.
- A different set of data can be transferred to the function each time if it is accessed.
- The use of a function avoids the need for redundant (repeated) programming of the same instructions.
- Functions provide the logical clarity resulting from the decomposition of the program into several concise functions.
- The use of functions also enables a program to build a customized library of frequently used routines.
- Function avoids repetitive programming between programs.
- It also promotes portability since programs can be written that are independent of system-dependent features.
Let us examine a function defined by the user.
int cube (int i) { Int retval; Retval = i* i * I; Return retvl; } |
This function is capable of returning a value (integer) since it is explicitly declared in the definition line. The word cube is the function name. The body of the function computes the cube of the value given to i and returns the integer type of value through the variable reveal.
# Write a program to find the cube of 10 during function call and cube of different integer value.
#include <stdio.h> #include <conio.h> int cube (int i) { int retval; retval = i*i*I; return retval; } void main () { int inputval; /* Cube of constant integer: */ /* function call with fixed value 10 as integer */ printf(“\n The cube of 10 is %d\n”, cube (10)); /*Cube of input integer: */ Printf(“\n Input an inter:”); scanf(“%d”,&inputval); printf(“\n The cube of given value %d is %d \n”, inputval, cube (inputval)); } |
You can predict the output. The program will give the cube value of 10 and cube of a given number.
Use of Function
The functions are used to enhance the program. The user defined functions help to calculate the values and return them. The functions can be used in different programs as required. The basic three fundamental properties of function are:
- Functions help to divide the program into a number of parts.
- Reduces the size of a program because it can be called independently.
- Makes easier to find errors and debug them.
While writing a program, it is split into smaller programs or functions in C. A main program is split into different programs or functions as shown below.
The major activities of the function while programming is:
- Function declaration
- Function call
- Function definition
1. Function Declaration
The function is generally written above the main() function. It is true that main is the main function without other functions cannot execute. It is the entry point. A complete function can be written before the main () function and that can be called from within the main() function.
The generalized concept about the declaration of function could be as follows:
- Declare function to inform the compiler that the function would use at the later point.
- Declare function at the beginning of a program.
- Function declaration includes function name, arguments, and their types, and return type of data.
Example
Void sum(); /*function declaration without parameter or arguments*/
Syntax:
Type specifier, function name (arguments);
Here,
Type specifier refers to the type of the function such as void, int, float, etc.
Function is the name of the function which can have as many as 40 characters arguments could be int, float, int pointer, etc. which indicates the type of return value in the function.
The function is generally declared before the main(). It may be defined anywhere in the program but it should be declared at the program.
2. Function Call
This is the part which executes the function. The function is called from within the main().
The function and its parameters are explicitly included within the function. Let us examine the function call as stated in above program. The last section of this line cube (inputval) is the function call. Although, it could be called in various ways, one of the simplest ways of calling function is shown at this level.
A function can be accessed or called by specifying its name followed by a list of arguments enclosed in the parentheses and separated by commas. If the function call does not require any arguments, an empty pair of parentheses must follow the name of the function.
The arguments appearing in the function call are referred to as actual arguments. In the normal function call, there will be one actual argument for each formal argument. The programmer needs to mention at least the following points about calling a function.
- Call from the main program with arguments or without arguments.
- Functionname (); indicates function defined.
- During function calling, control transfers to the called function. Eg. Sum(); calls sum() function.
3. Arguments
The actual arguments must correspond to the formal arguments in the function definition, i.e., the number of actual arguments must be the same as the number of formal arguments and each argument must be of the same data type as its corresponding formal argument.
- Arguments are the variables within the function.
- The arguments can be either passed by value or by reference.
- During argument passed by value, the numbers are explicitly defined.
- The references take the values through the variables.
References:
Khanal, R.C. Khanal, R.C. Computer Concept for XII. Pashupatigriha Marga, Thapathali, Kathmandu, Nepal: Ekta Books Distributors Pvt. Ltd., 2010. 194-197.
Adhikari, Deepak Kumar.,et.al., Computer Science XII,Asia Publication Pvt.Ltd
Lesson
Programming in C
Subject
Computer Science
Grade
Grade 12
Recent Notes
No recent notes.
Related Notes
No related notes.