
Functions in C: Usage and Examples
Table of Content:
- What is function
- Why we use function in C
- Types of functions
- The General Form of a Function
- Predefined standard library functions
- Predefined standard library functions
- Advantages of user-defined function
- Related Questions
- Related Assignment
- Stay Ahead of the Curve! Check out these trending topics and sharpen your skills.
What is function
A function is a self-contained block of statements that perform a coherent task of some kind. Every C program has at least one function, which is main() function.
Suppose you are building an application in C language and in one of your program, you need to perform
the same task more than once. In such case, you have two options –
a) Use the same set of statements every time you want to perform the task
b) Create a function to perform that task, and just call it every time you need to perform that task.
Using option (b) is a good practice and a good programmer always uses functions while writing codes in C.
Why we use function in C
A function is much important when we have to do a long project. Let we have a math function (sum, sub,mul etc.) in our project, we need the same calculation several times. if we have a function. We can do it by a single line (by calling the function). Otherwise, we will have to rewrite the same code of calculation again. And function got arbitrarily long. Above this reason we use function in C program and we don’t write all codes in main () function.
Functions are used because of following reasons –
a) To improve the readability of code.
b) Same function can be used in any program rather than
writing the same code from scratch.
c) Debugging of the code would be easier if you use functions, as errors are easy to be traced.
d) Reduces the size of the code, duplicate set of statements are replaced by function calls.
Types of functions
- Predefined standard library functions
- User Defined functions
1) Predefined standard library functions
Predefined standard library functions such as printf()
,
scanf()
, puts()
, gets()
These are the functions which already have a
definition in header files (.h files like stdio.h
),
so we just call them whenever there is a need to use them.
2) User Defined functions –
The functions that we create in a program are known as user-defined functions.
The General Form of a Function
return_type function_name (argument list) { Set of statements – Block of code }
return_type: Return type can be of any data type such as int, double, char, void, short etc. Don’t worry you will understand these terms better once you go through the examples below.
function_name: It can be anything, however, it is advised to have a meaningful name for the functions so that it would be easy to understand the purpose of the function just by seeing its name.
argument list: Argument list contains variables names along with their data types. These arguments are kind of inputs for the function. For example – A function which is used to add two integer variables will be having two integer argument.
Block of code: Set of C statements, which will be executed whenever a call will be made to the function.
Predefined standard library functions
The standard library functions are built-in functions in C programming to handle tasks such as mathematical computations, I/O processing, string handling etc.
These functions are defined in the header file. When you include the header file, these functions are available for use. For example:
The printf()
is a standard library function to send formatted output to the screen
(display output on the screen). This function is defined in header"stdio.h"
file.
There are other numerous library functions defined under "stdio.h"
, such as,
scanf()
fprintf()
, getchar()
etc.
Once you include "stdio.h"
in your program,
all these functions are available for use.
Program
#includevoid main(){ printf("Predefined standard library functions"); }
Predefined standard library functions
As mentioned earlier, C allows programmers to define functions. Such functions created by the user are called user-defined functions.
Depending on the complexity and requirement of the program, you can create as many user-defined functions as you want.
How user-defined function works?
#includevoid functionName() { ... .. ... ... .. ... } int main() { ... .. ... ... .. ... functionName(); ... .. ... ... .. ... }
The execution of a C program begins from the main()
function.
When the compiler encounters functionName();
inside the main function, control of the program jumps to
void functionName()
And, the compiler starts executing the codes inside the user-defined function.
The control of the program jumps to statement next to functionName();
once all
the codes inside the function definition are executed.
Program
#include/* function declaration */ int add(int num1, int num2); int main () { /* local variable definition */ int a = 100; int b = 200; int ret; /* calling a function to get add */ ret = add(a, b); printf( "value is : %d\n", ret ); return 0; } /* function returning the add between two numbers */ int add(int num1, int num2) { /* local variable declaration */ int result; result = num1+num2; return result; }
Output
value is : 300 Press any key to continue . . .
Advantages of user-defined function
- The program will be easier to understand, maintain and debug.
- Reusable codes that can be used in other programs
- A large program can be divided into smaller modules. Hence, a large project can be divided among many programmers.
- Question 1: What is the difference between actual and formal parameters?
- Question 2: Can a program be compiled without main() function?
- Question 3: Does a built-in header file contains built-in function definition?
- Question 4: what is modular programming.
- Question 5: Explain the use of %i format specifier w.r.t scanf().
- Question 6: How can you print a (backslash) using any of the printf() family of functions.
- Question 7: What is a static function?
- Question 8: How do you generate random numbers in C?
- Question 9: What could possibly be the problem if a valid function name such as tolower() is being reported by the C compiler as undefined?
- Question 10: What are actual arguments?
- Question 11: What does the function toupper() do?
- Question 12: Is it possible to have a function as a parameter in another function?
- Question 13: What is the difference between functions getch() and getche()?
- Question 14: Can you pass an entire structure to functions?
- Question 15: What is gets() function?
- Question 16: How do you search data in a data file using random access method?
- Question 17: Is there a built-in function in C that can be used for sorting data?
- Question 18: What is the use of the function in C?
Related Questions
- Assignment 1: C program to find power of a number using pow function
- Assignment 2: C Program - Prime Numbers Between Two Integers
- Assignment 3: C Program to Check Prime and Armstrong Number
- Assignment 4: Write a program that illustrates the scope rules in blocks.
- Assignment 5: Write a C program that illustrates the use local static variables and functions.
- Assignment 6: Create a power function using c
- Assignment 7: C Program - Integer as a Sum of Two Prime Numbers
- Assignment 8: Create a power function for negative exponent using c
- Assignment 9: power function in c programming language
- Assignment 10: Power function in c programming language || pow(2,i)
- Assignment 11: Power series of 2 using c programming language
- Assignment 12: Power series of 3 using c programming language