6.ICP - Subroutines - Functions
6.ICP - Subroutines - Functions
Types of functions
There are two types of functions which include:
● Library Functions – These are the functions which are
declared in the C header files such as scanf(), printf(),
fgets(), fputs() , fprintf() ,puts(),gets() ,fscanf(), malloc(),
calloc(), free() etc.
● User-defined functions – These are the functions which are
created by the programmer, so that he/she can use it many
times. It reduces the complexity of a big program and
optimises the code
1
Subroutine/Function Pseudocodes
Examples 1: Simple subroutine
SUBROUTINE addition()
num1 = 10
num2 = 23
return num1 + num 2
ENDSUBROUTINE
OUTPUT addition()
2
Function declaration
● Function declaration is telling the compiler about a function
name and how to call the function.
● The actual body of the function can be defined separately.
Syntax
return_type function_name( parameter list );
Example
int addition(int num1, int num2);
3
Calling a function
● While creating a function, you give a definition of what the
function has to do. To use a function, you will have to call
that function to perform the defined task.
● When a program calls a function, program control is
transferred to the called function. A called function performs
defined task and when its return statement is executed or
when its function ending closing brace is reached, it returns
program control back to the main program.
● To call a function, you simply need to pass the required
parameters along with function name, and if function returns
a value, then you can store returned value.
● Function can be called from anywhere in the program. The
parameter list must not differ in function calling and function
declaration. We must pass the same number of functions as
it is declared in the function declaration.
4
Defining Functions
● Function definition is the creation (space reservation) of a
function based on its description (name, parameters, return
value, variables defined inside the function and statements
that comprise the body of the function).
● A function definition in C programming language consists of
a function header and a function body.
Parts of a function
5
Syntax of function definition
return_type function_name
{
Block of code
}
6
int addition(int num1,int num2); // function declaration.
int main() {
int var1,var2;
printf("Enter first number :");
scanf("%d",&var1);
7
int divide(int dividend,int divisor, int* result);
int main() {
int result;
int ret = divide(10, 0, &result);
if (ret == -1) {
printf("Error: Division by zero is not allowed\n");
} else {
printf("Result: %d\n", result);
}
return 0;
}
8
I. Subroutines or functions can be reused multiple times within
a program or in different programs, reducing redundant code
and increasing efficiency.
II. Separating code into smaller, manageable units makes it
easier to debug, update, and maintain the codebase.
III. Breaking down complex tasks into smaller functions
improves the readability and understanding of the code,
making it easier for developers to collaborate and work on
the codebase.
IV. Functions allow for the encapsulation of specific functionality,
limiting the scope of variables and operations within a
specific context and reducing potential errors and conflicts.
V. Structuring code into functions promotes modularity, allowing
developers to work on individual components independently
and facilitating code organisation and management.
VI. Functions allow for code reuse, reducing the need to rewrite
the same logic multiple times and saving development time.
VII. Breaking down the code into smaller, more manageable
functions can make it easier to isolate and debug issues,
leading to faster troubleshooting.
VIII. Functions promote code reusability and help in avoiding
redundant code, leading to a more efficient and concise
codebase.
IX. Function encapsulation limits the scope of variables and
operations, reducing the chances of errors and making the
code more robust.
X. Modifying a program becomes easier: Functions make it
easier to modify and update specific parts of the code
without impacting the entire program, enhancing code
flexibility and maintainability.
9
Disadvantages of structuring code into subroutines or
functions.
10
require a good understanding of programming concepts and
best practices, which may pose a challenge for novice
programmers.
References
1.Mathcs.emory.edu. 2022. [online] Available at:
<http://www.mathcs.emory.edu/~cheung/Courses/561/Syllab
us/2-C/subroutine.html#:~:text=Subroutine%2FFunction%20i
s%20a%20programming,instructions%20with%20a%20speci
fic%20name.&text=After%20its%20definition%2C%20the%2
0subroutine,many%20times%20as%20is%20desired.>
[Accessed 18 January 2022].
11
2.www.javatpoint.com. 2022. Functions in C - javatpoint.
[online] Available at:
<https://www.javatpoint.com/functions-in-c> [Accessed 19
January 2022].
3.Tutorialspoint.com. 2022. C - Functions. [online] Available at:
<http://www.tutorialspoint.com/cprogramming/c_functions.ht
m> [Accessed 19 January 2022].
4.Ibm.com. 2022. IBM Docs. [online] Available at:
<https://www.ibm.com/docs/en/i/7.4?topic=extensions-stand
ard-c-library-functions-table-by-name> [Accessed 19
January 2022].
12