0% found this document useful (0 votes)
16 views

Programming in C - 81-100

This document discusses functions in C programming. It explains what functions are, function basics like defining and calling functions, function prototypes, recursion, and the philosophy of breaking programs into functions. Some example programs are also provided to illustrate key concepts around functions.

Uploaded by

SrinivasaRao
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Programming in C - 81-100

This document discusses functions in C programming. It explains what functions are, function basics like defining and calling functions, function prototypes, recursion, and the philosophy of breaking programs into functions. Some example programs are also provided to illustrate key concepts around functions.

Uploaded by

SrinivasaRao
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

Programming in C Unit 4

4.9 The break statement and continue statement


The purpose of break statement is to break out of a loop (while, do while,
or for loop) or a switch statement. When a break statement is encountered
inside a loop, the loop is immediately exited and the program continues with
the statement immediately following the loop. When the loops are nested,
the break would only exit from the loop containing it. That is, the break
would exit only a single loop.
Syntax : break;

Program 4.9: Program to illustrate the use of break statement.


void main ( )
{
int x;
for (x=1; x<=10; x++)
{
if (x==5)
break; /*break loop only if x==5 */
printf(“%d”, x);
}
printf (“\nBroke out of loop”);
printf( “at x =%d“);
}
The above program displays the numbers from 1to 4 and prints the
message “Broke out of loop when 5 is encountered.

The continue statement


The continue statement is used to continue the next iteration of the loop by
skipping a part of the body of the loop (for, do/while or while loops). The
continue statement does not apply to a switch, like a break statement.
Unlike the break which causes the loop to be terminated, the continue,
causes the loop to be continued with the next iteration after skipping any
statements in between.

Syntax: continue;

Manipal University Jaipur Page No.: 81


Programming in C Unit 4

Program 4.10: Program to illustrate the use of continue statement.


void main ( ) {
int x;
for (x=1; x<=10; x++)
{ if (x==5)
continue; /* skip remaining code in loop
only if x == 5 */
printf (“%d\n”, x);
}
printf(“\nUsed continue to skip”);
}
The above program displays the numbers from 1to 10, except the number 5.

Program 4.11: Program to sum integers entered interactively


#include <stdio.h>
int main(void)
{
long num;
long sum = 0L; /* initialize sum to zero */
int status;

printf("Please enter an integer to be summed. ");


printf("Enter q to quit.\n");
status = scanf("%ld", &num);
while (status == 1)
{
sum = sum + num;
printf("Please enter next integer to be summed. ");
printf("Enter q to quit.\n");
status = scanf("%ld", &num);
}
printf("Those integers sum to %ld.\n", sum);
return 0;
}

Manipal University Jaipur Page No.: 82


Programming in C Unit 4

4.10 Summary
In C by default, statements are executed in sequence, one after another.
We can, however, modify that sequence by using control flow constructs.
C language possesses decision making capabilities and supports the
following statements known as the control or decision making statements:
goto, if, switch. The goto statement is used to branch unconditionally from
one point to another in the program. The simplest way to modify the control
flow of a program is with an if statement. switch statements serve as a
simple way to write long if statements when the requirements are met.
The most basic loop in C is the while loop. A while loop has one control
expression, and executes as long as that expression is true. do..while loop
is used in a situation where we need to execute the body of the loop before
the test is performed. The for loop is used to execute the set of statements
repeatedly for a fixed number of times. It is an entry controlled loop. break
statement is used to exit any loop. Unlike the break which causes the loop
to be terminated, the continue, causes the loop to be continued with the
next iteration after skipping any statements in between.

4.11 Terminal Questions


1. Consider the following program segment:
x=1;
y=1;
if (n>0)
x=x+1;
y=y-1;
printf(“%d %d”,x,y);
What will be the values of x and y if n assumes a value of (a) 1 and
(b) 0.
2. Rewrite the following without using compound relation:
if (grade<=59 && grade>=50)
second = second +1;
3. Write a program to check whether an input number is odd or even.

Manipal University Jaipur Page No.: 83


Programming in C Unit 4

4. Write the output that will be generated by the following C program:


void main()
{
int i=0, x=0;
while (i<20)
{
if (i%5 == 0)
{
x+=i;
printf(“%d\t”, i);
}
i++;
}
printf(“\nx=%d”; x);
}

5. Write the output that will be generated by the following C program:


void main()
{
int i=0, x=0;
do
{
if (i%5 == 0)
{
x++;
printf(“%d\t”, x);
}
++i;
} while (i<20);
printf(“\nx=%d”, x);
}

4.12 Answers to Self Assessment Questions


1. label
2. true
3. false

Manipal University Jaipur Page No.: 84


Programming in C Unit 4

4. if
5. true
6. value
7. false
8. while
9. true
10. do…while
11. false
12. false
13. true

4.13 Answers to Terminal Questions


1. (a) x=2, y=0
(b) x=1, y=0
2. if (grade<=59)
if (grade>=50)
second = second+1;
3. void main()
{
int no;
printf(“enter a number\n”);
scanf(“%d”,&no);
if (no%2==0)
printf(“even number\n”);
else printf(“odd number\n”);
}

4. 0 5 10 15
x = 30
5. 1 2 3 4
x=4

Manipal University Jaipur Page No.: 85


Programming in C Unit 4

4.14 Exercises
1. Explain different types of if statements with examples.
2. Explain the syntax of switch statement with an example.
3. Write a program to check whether a given number is odd or even using
switch statement.
4. Write a program to find the smallest of 3 numbers using if-else
statement.
5. Write a program to find the roots of a quadratic equation.
6. Compare the following statements
a) while and do…while
b) break and continue
7. Write a program to compute the sum of digits of a given number using
while loop.
8. Write a program that will read a positive integer and determine and
print its binary equivalent using do…while loop.
9. The numbers in the sequence
1 1 2 3 5 8 13 ………..
are called Fibonacci numbers. Write a program using do…while loop
to calculate and print the first n Fibonacci numbers.
10. Find errors, if any, in each of the following segments. Assume that all
the variables have been declared and assigned values.
(a)
while (count !=10);
{
count = 1;
sum = sum + x;
count = count + 1;
}

(b)
do;
total = total + value;
scanf(“%f”, &value);
while (value ! =999);

Manipal University Jaipur Page No.: 86


Programming in C Unit 4

11. Write programs to print the following outputs using for loops.
(a) 1 b) 1
22 2 2
333 3 3 3
4444 4 4 4 4
12. Write a program to read the age of 100 persons and count the number
of persons in the age group 50 to 60. Use for and continue statements.
13. Write a program to print the multiplication table using nested for loops.

Manipal University Jaipur Page No.: 87


Programming in C Unit 5

Unit 5 Functions
Structure:
5.1 Introduction
Objectives
5.2 Function Basics
5.3 Function Prototypes
5.4 Recursion
5.5 Function Philosophy
5.6 Summary
5.7 Terminal Questions
5.8 Answers for Self Assessment Questions
5.9 Answers for Terminal Questions
5.10 Exercises

5.1 Introduction
In the previous unit, you studied about the control statements and its usage
in C. You also studied how those control statements are helpful in making
decisions when various types of conditions and options are available in the
problem. In this unit, you will study about what a function is.
A function is a “black box'' that we've locked part of our program into. The
idea behind a function is that it compartmentalizes part of the program, and
in particular, that the code within the function has some useful properties:
It performs some well-defined task, which will be useful to other parts of the
program. It might be useful to other programs as well; that is, we might be
able to reuse it (and without having to rewrite it).
The rest of the program doesn't have to know the details of how the function
is implemented. This can make the rest of the program easier to think about.
The function performs its task well. It may be written to do a little more than
is required by the first program that calls it, with the anticipation that the
calling program (or some other program) may later need the extra
functionality or improved performance. (It's important that a finished function
do its job well, otherwise there might be a reluctance to call it, and it
therefore might not achieve the goal of reusability.)

Manipal University Jaipur Page No.: 88


Programming in C Unit 5

By placing the code to perform the useful task into a function, and simply
calling the function in the other parts of the program where the task must be
performed, the rest of the program becomes clearer: rather than having
some large, complicated, difficult-to-understand piece of code repeated
wherever the task is being performed, we have a single simple function call,
and the name of the function reminds us which task is being performed.
Since the rest of the program doesn't have to know the details of how the
function is implemented, the rest of the program doesn't care if the function
is re-implemented later, in some different way (as long as it continues to
perform its same task, of course!). This means that one part of the program
can be rewritten, to improve performance or add a new feature (or simply to
fix a bug), without having to rewrite the rest of the program.
Functions are probably the most important weapon in our battle against
software complexity. You'll want to learn when it's appropriate to break
processing out into functions (and also when it's not), and how to set up
function interfaces to best achieve the qualities mentioned above: reusability,
information hiding, clarity, and maintainability.
Objectives:
After studying this unit, you should be able to:
 explain the importance of functions
 implement the concepts of formal arguments and actual arguments
 explain function declaration(function prototypes) and function definition
 use the concept of recursion
 explain how the concept of functions reduces software complexity

5.2 Function Basics


A function is a self-contained program segment that carries out some
specific, well-defined task. Every C program contains one or more functions.
One of these functions must be called main. Program execution will always
begin by carrying out the instructions in main. Additional functions will be
subordinate to main, and perhaps to one another.
So what defines a function? It has a name that you call it by, and a list of
zero or more arguments or parameters. Parameters (also called formal
parameters) or arguments are the special identifiers through which

Manipal University Jaipur Page No.: 89


Programming in C Unit 5

information can be passed to the function. A function has a body containing


the actual instructions (statements) for carrying out the task the function is
supposed to perform; and it may give you back a return value, of a particular
type.
In general terms, the first line can be written as
data-type name(data-type parameter 1, data-type parameter 2, …, data-type
parameter n)
Example 5.1: Here is a very simple function, which accepts one
argument, multiplies it by 4, and hands that value back.
int multbyfour(int x)
{
int retval;
retval = x * 4;
return retval;
}

On the first line we see the return type of the function (int), the name of the
function (multbyfour), and a list of the function's arguments, enclosed in
parentheses. Each argument has both a name and a type; multbyfour
accepts one argument, of type int, named x. The name x is arbitrary, and is
used only within the definition of multbyfour. The caller of this function only
needs to know that a single argument of type int is expected; the caller does
not need to know what name the function will use internally to refer to that
argument. (In particular, the caller does not have to pass the value of a
variable named x.)
Next we see, surrounded by the familiar braces, the body of the function
itself. This function consists of one declaration (of a local variable retval) and
two statements. The first statement is a conventional expression statement,
which computes and assigns a value to retval, and the second statement is
a return statement, which causes the function to return to its caller, and also
specifies the value which the function returns to its caller.
In general term, a return statement is written as
return expression

Manipal University Jaipur Page No.: 90


Programming in C Unit 5

The return statement can return the value of any expression, so we don't
really need the local retval variable; this function can also be written as
int multbyfour(int x)
{
return x * 4;
}

How do we call a function? We've been doing so informally since day one,
but now we have a chance to call one that we've written, in full detail. The
arguments in the function call are referred to as actual arguments or actual
parameters, in contrast to the formal arguments that appear in the first line
of the function definition.
Here is a tiny skeletal program to call multbyfour:
#include <stdio.h>
extern int multbyfour(int);
int main()
{
int i, j;
i = 5;
j = multbyfour(i);
printf("%d\n", j);
return 0;
}

This looks much like our other test programs, with the exception of the new
line
extern int multbyfour(int);
This is an external function prototype declaration. It is an external
declaration, in that it declares something which is defined somewhere else.
(We've already seen the defining instance of the function multbyfour, but
may be the compiler hasn't seen it yet.) The function prototype declaration
contains the three pieces of information about the function that a caller
needs to know: the function's name, return type, and argument type(s).
Since we don't care what name the multbyfour function will use to refer to

Manipal University Jaipur Page No.: 91


Programming in C Unit 5

its first argument, we don't need to mention it. (On the other hand, if a
function takes several arguments, giving them names in the prototype may
make it easier to remember which is which, so names may optionally be
used in function prototype declarations.) Finally, to remind us that this is an
external declaration and not a defining instance, the prototype is preceded
by the keyword extern.
The presence of the function prototype declaration lets the compiler know
that we intend to call this function, multbyfour. The information in the
prototype lets the compiler generate the correct code for calling the function,
and also enables the compiler to check up on our code (by making sure, for
example, that we pass the correct number of arguments to each function we
call).
Down in the body of main, the action of the function call should be obvious:
the line
j = multbyfour(i);
calls B, passing it the value of i as its argument. When multbyfour returns,
the return value is assigned to the variable j. (Notice that the value of main's
local variable i will become the value of multbyfour's parameter x; this is
absolutely not a problem, and is a normal sort of affair.)
This example is written out in “longhand,'' to make each step equivalent.
The variable i isn't really needed, since we could just as well call
j = multbyfour(5);
And the variable j isn't really needed, either, since we could just as well call
printf("%d\n", multbyfour(5));
Here, the call to multbyfour is a subexpression which serves as the second
argument to printf. The value returned by multbyfour is passed
immediately to printf. (Here, as in general, we see the flexibility and
generality of expressions in C. An argument passed to a function may be an
arbitrarily complex subexpression, and a function call is itself an expression
which may be embedded as a subexpression within arbitrarily complicated
surrounding expressions.)

Manipal University Jaipur Page No.: 92


Programming in C Unit 5

We should say a little more about the mechanism by which an argument is


passed down from a caller into a function. Formally, C is call by value, which
means that a function receives copies of the values of its arguments. We
can illustrate this with an example. Suppose, in our implementation of
multbyfour, we had gotten rid of the unnecessary retval variable like this:
int multbyfour(int x)
{
x = x * 4;
return x;
}

We might wonder, if we wrote it this way, what would happen to the value of
the variable i when we called
j = multbyfour(i);
When our implementation of multbyfour changes the value of x, does that
change the value of i up in the caller? The answer is no. x receives a copy
of i's value, so when we change x we don't change i.
However, there is an exception to this rule. When the argument you pass to
a function is not a single variable, but is rather an array, the function does
not receive a copy of the array, and it therefore can modify the array in the
caller. The reason is that it might be too expensive to copy the entire array,
and furthermore, it can be useful for the function to write into the caller's
array, as a way of handing back more data than would fit in the function's
single return value. We will discuss more about passing arrays as
arguments to a function later.
There may be several different calls to the same function from various
places within a program. The actual arguments may differ from one function
call to another. Within each function call, however, the actual arguments
must correspond to the formal arguments in the function definition; i.e the
actual arguments must match in number and type with the corresponding
formal arguments.

Manipal University Jaipur Page No.: 93


Programming in C Unit 5

Program 5.1: A program to find the largest of three integers


#include<stdio.h>
main()
{
int x, y, z, w;
/* read the integers */
int max(int, int);
printf(“\nx= “);
scanf(“%d”, &x);
printf(“\ny= “);
scanf(“%d”, &y);
printf(“\nz= “);
scanf(“%d”, &z);
/* Calculate and display the maximum value */
w= max(x,y);
printf(“\n\nmaximum=%d\n”, max(z,w));
}
int max(int a, int b)
{
int c;
c=(a>=b)?a:b;
return c;
}
Please execute this program and observe the result.
Function calls can span several levels within a program; function A can call
function B, which can call function C and so on.
Program 5.2: Program to check whether a given integer is a perfect
square or not.
#include<stdio.h>
main()
{
int psquare(int);
int num;
printf(“ Enter the number:”);

Manipal University Jaipur Page No.: 94


Programming in C Unit 5

scanf(“%d”, &num);
if(psquare(num)) /* main() calls the function psquare() */
{
printf(“%d is a perfect square\n”);
else
printf(“%d is not a perfect square\n”);
}
}

int psquare(int x)
{
int positive(int);
float sqr;
if(positive(x)) /* psquare() in turn calls the function positive() */
{
sqr=sqrt(x));
if(sqr-int(sqr))==0)
return 1;
else
return 0;
}
int positive(int m)
{
if(m>0)
return 1;
else return 0;
}

Execute the above program and observe the result.


In the above program the main function calls the function psquare() and it
in turn calls the function positive() to check whether the number to be
checked for perfect square is a positive or not before checking.
The return statement can be absent altogether from a function definition,
though this is generally regarded as a poor programming practice. If a
function reaches end of the block without encountering a return statement,
control simply reverts back to the calling portion of the program without

Manipal University Jaipur Page No.: 95


Programming in C Unit 5

returning any information. Using an empty return statement(without the


accompanying expressions) is recommended.
Example 5.2: The following function accepts two integers and
determines the larger one, which is then written out. The function
doesn’t return any information to the calling program.
void max(int x, int y)
{
int m;
m=(x>=y)?x:y;
printf(“ The larger integer is=%d\n”, m);
return;
}
Self Assessment Questions
1. The function main() is optional in a C program. (True/False)
2. If the function is defined elsewhere (not in the same program where it is
called), the function prototype must be preceded by the keyword
_____________.
3. The arguments that appear in function definition are called
_____________ arguments whereas the arguments that appear when
the function is called are the ___________ arguments.
4. The return data type, function name and the list of formal parameters
enclosed in brackets are separated by _______________.

5.3 Function Prototypes


In modern C programming, it is considered good practice to use prototype
declarations for all functions that you call. As we mentioned, these
prototypes help to ensure that the compiler can generate correct code for
calling the functions, as well as allowing the compiler to catch certain
mistakes you might make.
In general terms, a function prototype can be written as
data-type name(type1, type2, …, type n)
Examples:
int sample(int, int) or int sample(int a, int b);
float fun(int, float) or float fun( int a, float b);

Manipal University Jaipur Page No.: 96


Programming in C Unit 5

void demo(void); Here void indicates function neither return any value to the
caller nor it has any arguments.
If you write the function definition after the definition of its caller function,
then the prototype is required in the caller, but the prototype is optional if
you write the definition of the function before the definition of the caller
function. But it is good programming practice to include the function
prototype wherever it is defined.
If prototypes are a good idea, and if we're going to get in the habit of writing
function prototype declarations for functions we call that we've written (such
as multbyfour), what happens for library functions such as printf? Where
are their prototypes? The answer is in that boilerplate line
#include <stdio.h>
we've been including at the top of all of our programs. stdio.h is conceptually
a file full of external declarations and other information pertaining to the
“Standard I/O'' library functions, including printf. The #include directive
arranges all of the declarations within stdio.h that are considered by the
compiler, rather as if we'd typed them all in ourselves. Somewhere within
these declarations is an external function prototype declaration for printf,
which satisfies the rule that there should be a prototype for each function we
call. (For other standard library functions we call, there will be other “header
files'' to include.) Finally, one more thing about external function prototype
declarations: we've said that the distinction between external declarations
and defining instances of normal variables hinges on the presence or
absence of the keyword extern. The situation is a little bit different for
functions. The “defining instance'' of a function is the function, including its
body (that is, the brace-enclosed list of declarations and statements
implementing the function). An external declaration of a function, even
without the keyword extern, looks nothing like a function declaration.
Therefore, the keyword extern is optional in function prototype declarations.
If you wish, you can write
int multbyfour(int);
and this is just like an external function prototype declaration as
extern int multbyfour(int);

Manipal University Jaipur Page No.: 97


Programming in C Unit 5

(In the first form, without the extern, as soon as the compiler sees the
semicolon, it knows it's not going to see a function body, so the declaration
can't be a definition.) You may want to stay in the habit of using extern in all
external declarations, including function declarations, since “extern =
external declaration'' is an easier rule to remember.
Program 5.3: Program to illustrate that the function prototype is
optional in the caller function. The program is to convert a character
from lowercase to uppercase.
#include<stdio.h>
char lower_to_upper(char ch) /* Function definition precedes main()*/
{
char c;
c=(ch>=’a’ && ch<=’z’) ? (‘A’+ch-‘a’):ch;
return c;
}

main()
{
char lower, upper;
/* char lower_to_upper(char lower); */ /* Function prototype is
optional here*/
printf(“Please enter a lowercase character:”);
scanf(“%c”, &lower);
upper=lower_to_upper(lower);
printf(“\nThe uppercase equivalent of %c is %c\n”, lower, upper);
}

Self Assessment Questions


5. Function prototype is also called ________.
6. The function prototypes are optional. (True/False)
7. The function prototypes of the library functions is in the corresponding
__________ files.
8. The function prototype for the function fun() called in main() below is
________________ .

Manipal University Jaipur Page No.: 98


Programming in C Unit 5

main()
{
double x, y, z;

z=fun(x, y);

}

5.4 Recursion
Recursion is a process by which a function calls itself repeatedly, until some
specified condition has been met. The process is used for repetitive
computations in which each action is stated in terms of a previous result.
Many repetitive problems can be written in this form.
In order to solve a problem recursively, two conditions must be satisfied.
First, the problem must be written in a recursive form, and the second, the
problem statement must include a stopping condition.
Example 5.3: Factorial of a number. Suppose we wish to calculate the
factorial of a positive integer, n. We would normally express this problem as
n!=1 x 2 x 3 x … x n.
This can also be written as n!=n x (n-1)!. This is the recursive statement of
the problem in which the desired action(the calculation of n!) is expressed in
terms of a previous result (the value of (n-1)! which is assumed to be
known). Also, we know that 0!=1 by definition. This expression provides
stopping condition for the recursion.
Thus the recursive definition for finding factorial of positive integer n can be
written as:
fact(n)={ 1 if n=0
n x fact(n-1) otherwise}
Program 5.4: Program to find factorial of a given positive integer
#include<stdio.h>
main()
{
int n;
long int fact(int);

Manipal University Jaipur Page No.: 99


Programming in C Unit 5

/* Read in the integer quantity*/


scanf(“%d”, &n);
/*calaculate and display the factorial*/
printf(“n!=%ld\n”, fact(n));
}
long int fact(int n)
{
if(n==0)
return(1);
else
return (n*fact(n-1));
}
Please execute this program and observe the result.
Example 5.4: The Towers of Hanoi. The Towers of Hanoi is a game
played with three poles and a number of different sized disks. Each disk has
a hole in the center, allowing it to be stacked around any of the poles.
Initially, the disks are stacked on the leftmost pole in the order of decreasing
size, i.e, the largest on the bottom, and the smallest on the top as illustrated
in Figure 5.1.

Figure 5.1: Illustration for Tower of Hanoi

The aim of the game is to transfer the disks from the leftmost pole to the
rightmost pole, without ever placing a larger disk on top of a smaller disk.
Only one disk may be moved at a time, and each disk must always be
placed around one of the poles.
Manipal University Jaipur Page No.: 100

You might also like