Chapter 9 Functions and Sub-Routines

Functions and Sub-Routines Long Answer Type Questions (8 Marks)

Question 1.
What do you mean by function in ‘C’? (UP 2004, 08, 09, 10, 19)
Or
What is the library function in ‘C’? Explain. (UP 2010, 12, 18)
Introduction: Sometimes, to perform a particular task in a program, you may have to write another program which may be a very cumbersome process. Functions are very useful to read, write, debug and modify complex programs. They can also be easily incorporated in the main program. In ‘C’ language, main() itself is a function that means the main function is invoking the other functions to perform various tasks.
There are two types of functions:

1. Built-in functions/Library Function.
2. User-defined functions.

1. Built-in Functions/Library Functions: These are the pre-defined routines that are included as an integral part of the language. Each function is accessed simply by stating its name, followed by whatever information must be supplied to the function, enclosed in parentheses (A numeric quantity or a string that is passed to a function in this manner is called an argument). Library functions are also called standard functions or elementary functions. These functions provide a quick and easy way to evaluate many mathematical functions and to carry out certain logical operations.
There are several library functions in ‘C’ Language. Basic Library functions are of two types:

• String Functions: String functions operate on strings, e.g., strlen (), strrev()
• Numeric Functions: Numeric functions operate on numeric values, e.g., ABS(), EXP(), SIN(), cos().

2. User-defined Functions: This feature allows you to define functions within a program, which you can call whenever you need them. While running the program (FUNC) statement can save space as well time, as a complex calculation can be defined with a short name and called up by its name where needed.

Question 2.
What are user-defined functions? Explain with example. (UP 2005, 13, 16)
User-defined Functions: This feature allows you to define functions within a program, which you can call whenever you need them. While running the program (FUNC) statement can save space as well as time, as a complex calculation can be defined with a short name and called up by its name where needed.
Defining a Function

main()

{

printf (“I am in function main \n”);

India();

printf (“I am finally back in function main \n”);

}

India()

{

printf (“I am in India \n”);

Delhi();

printf (“I am back in India \n”);

1

Delhi();

{

printf (“I am in Delhi \n”);

Bombay();

}

Bombay()

{

printf (“I am in Bombay”);

}

Output:
I am in function main
I am in India
I am in Delhi
I am in Bombay
I am back in India
I am finally back in function main

Question 3.
WAP to demonstrate a function declaration, function calling and function definition.
Program:

#include<stdio.h>

void main (void)

{

void display (void); /* function declaration */ display (); /* function calling */

}

void display (void)/* function definition */

{

printf (“this is a test program \n”);

printf (“for demonstrating a function call \n”);

printf (“in C \n”);

}

Output:
this is a test program for demonstrating a function call in C

Question 4.
Explain Return statement with example.
Return Statement: The keyword return is used to terminate function and return a value to its caller. The return statement may also be used to exit a function without returning a value. The return statement may or may not include an expression.
Example Program:

# include<stdio.h>

void main (void)

{

float max (float, float, float);

float a, b, c maximum;

printf (“Enter three numbers\n”);

scanf (“%d %d %d”, &a, &b &c);

maximum = max (a, b, c);

printf(“maximum = %d, maximum);

}

float max (float x, float y, float z)

}

if (x > y)

{

if (x > z)

return (x);

}

else

{

if (y > z)

return (z);

else

return (z);

}

}

Output:
Enter three numbers
2 4 8
maximum = 8

Functions and Sub-Routines Short Answer Type Questions (4 Marks)

Question 1.
What is the status of ‘C’ language? Why is ‘C’ language so popular in the software world? Explain. (UP 2011, 15, 18, 19)
Or
Why is ‘C’ a very popular language? Explain with suitable example. (UP 2017)
Programming Language is a set of instructions and keywords which help a user to communicate with the computer. The ‘C’ programming language was developed by Dennis Ritchie in Bell Telephone Laboratories, in the early 1970s. Ken Thompson and his team supported him in developing this language. In the late seventies, ‘C’ began to replace the more familiar languages of that time like PL/I, ALGOL, etc. In less than a decade of its introduction, ‘C’ has become a language of choice for software professionals because it is reliable, simple and easy to use. The UNIX operating system was written in ‘C’ language.

One of the most interesting things about the ‘C’ language is that its compiler is written in ‘C’. ‘C’ language is considered as a middle-level language since it was designed to have both a relatively good programming efficiency and a relatively good machine efficiency. It is a bridge between low level and high-level languages.

• ‘C’ language possesses features of both low level and high-level languages.
• It provides the facility to do programming with easy approaches.
• It provides the facility to do programming at register level which leads to fast processing.
• It consists of simple English like commands, which are easy to understand.
• The programs written in ‘C’ language are 100 per cent efficient and are also machine-independent and portable.
• A ‘C’ program is easier to write and does not vary much from computer to computer.

Question 2.
Define ABS function with example.
ABS(X): This function gives the absolute value of X. The absolute value of a constant is always independent of its sign le., it is always positive.
For example:

#include<stdio.h>

#include<conio.h>

void main()

{

int a = -20, b = -20.23, c = 18.5

printf (“%d %d %d”, abs(a), abs(b), abs(c));}

Output: 20 20.23 18.5

Question 3.
Define SQR function with example.
SQR(X): function returns the square root of its argument which should never be negative.
For example:

#include<stdio.h>

#include<conio.h>

void main()

{

int a = 4, b;

b = sqr(a);

printf (“%d”, b);

}

Output: 2

Functions and Sub-Routines Very Short Answer Type Questions (2 Marks)

Question 1.
What is argument?
The value we pass in a function at the time of its calling is known as an argument.

Question 2.
What is Numeric function? (UP 2016)
The numeric function works on numeric values.

Question 3.
What is the use of EXP function?
EXP function is used to return the exponential value of the argument.

Question 4.
What is the other name of the pre-defined function?
The pre-defined function is also known as built-in function.

Question 5.
Who developed ‘C’ language?
Dennis Ritchie and Ken Thompson.

Question 6.
A loop within a loop is known as ……..
Nested loop.

Functions and Sub-Routines Objective Type Questions (1 Marks)

There are four alternative answers for each part of the questions. Select the correct one and write in your answer book:

Question 1.
Functions which are compiled into object modules are known as (UP 2012)
(a) Library functions
(b) Recursive functions
(c) User-defined functions
(d) None of these.
(a) Library functions

Question 2.
The default return type of a user-defined function is:
(a) void
(b) float
(c) char
(d) int
(a) void

Question 3.
A user-defined function can be called:
(a) One time
(b) Two times
(c) Three times
(d) More than three times.
(d) More than three times.

Question 4.
Language used to design web pages is: (UP 2014)
(a) C++
(b) BASIC
(c) HTML
(d) JAVA.
(c) HTML

Question 5.
The value passed at the time of calling a function is known as:
(a) Variables
(b) Constants
(c) Arguments
(d) None of these.