0% found this document useful (0 votes)
10 views12 pages

6.ICP - Subroutines - Functions

Uploaded by

Leo Nembaware
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views12 pages

6.ICP - Subroutines - Functions

Uploaded by

Leo Nembaware
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Subroutine/Function

●​ Subroutine/Function is a programming construct that allows


a programmer to associate a given set of instructions with a
specific name.
●​ A function consists of a (unique) name and a function body.
●​ The function can optionally have one or more parameters
that can affect the behaviour of the subroutine/function
●​ Function is defined once.
●​ After its definition, the function can be used (called or
invoked) many times as is desired.

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()

Example 2: Subroutine with parameters.


SUBROUTINE addition(num1,num2)
return num1+num2
ENDSUBROUTINE
OUTPUT addition(10,23)

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.

Syntax for calling a function


function_name (argument_list)
e.g. 1 Calling a function by value.
addition(var1,var2)
e.g.2 Calling a function by reference.
addition(&var1,&var2)

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

●​ Return Type: A function may return a value. The


return_type is the data type of the value the function
returns. Some functions perform the desired operations
without returning a value. In this case, the return_type is the
keyword void.
●​ Function Name: This is the actual name of the function. The
function name and the parameter list together constitute the
function signature.
●​ Parameters: A parameter is like a placeholder. When a
function is invoked/ called on, you pass a value to the
parameter. This value is referred to as actual parameter or
argument. The parameter list refers to the type, order, and
number of the parameters of a function. Parameters are
optional; that is, a function may contain no parameters.
●​ Function Body: The function body contains a collection of
statements that define what the function does.

5
Syntax of function definition
return_type function_name
{
Block of code
}

Highlevel language example


int addition(int num1,int num2)
{
return num1 +num2;
}

/*Example program for calculating sum of two numbers using a


function*/
#include<stdio.h>

6
int addition(int num1,int num2); // function declaration.
int main() {
int var1,var2;
printf("Enter first number :");
scanf("%d",&var1);

printf("Enter Second number :");


scanf("%d",&var2);

//Calling a function by value


printf("The result is %d",addition(var1,var2));
return 0;
}

//addition function/subroutine definition


int addition(int num1, int num2)
{
return num1 + num;
}

/*Example program with a method to indicate error or failure to the


caller*/
#include <stdio.h>

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;
}

int divide(int dividend, int divisor, int* result) {


if (divisor == 0) {
return -1; // Error code for division by zero
}
*result = dividend / divisor;
return 0; // Success code
}

Advantages of structuring code into subroutines or


functions:

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.

I.​ Introducing too many functions can create overhead in terms


of memory consumption and performance, especially if the
functions are small and called frequently.
II.​ Excessive abstraction through functions can make the code
harder to understand and follow, leading to potential
confusion and inefficiency in code maintenance.
III.​ Calling functions involves some overhead in terms of context
switching and memory allocation, which can impact
performance for large-scale applications.
IV.​ Splitting code into multiple functions can make it challenging
to track the flow of execution, especially in complex
programs with numerous function calls and dependencies.

V.​ Testing individual functions in isolation can be easier, but


integration testing and ensuring the correct interactions
between functions can be more challenging and
time-consuming.
VI.​ Using too many functions in a program can lead to a
complex code structure that may be difficult to understand
and maintain, especially for inexperienced developers.
VII.​ Calling a function is slower than running the code inside the
function: Function call overhead can impact performance,
especially in programs with frequent function calls or
complex dependencies.
VIII.​ Programmer must be an expert to use subroutines i.e.
Developing and managing a large number of 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

You might also like