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

class_6_programming_in_c_winter202425

The document provides an introduction to functions in C programming, explaining how to define and use functions, including examples of basic function usage, reusability, and the use of arguments and local variables. It also covers functions that operate on arrays and introduces recursive functions with a factorial example. Additionally, it includes questions for further practice on creating user-defined libraries and writing specific functions.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

class_6_programming_in_c_winter202425

The document provides an introduction to functions in C programming, explaining how to define and use functions, including examples of basic function usage, reusability, and the use of arguments and local variables. It also covers functions that operate on arrays and introduces recursive functions with a factorial example. Additionally, it includes questions for further practice on creating user-defined libraries and writing specific functions.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

EC2025E INTRODUCTION TO C PROGRAMMING

Department of Electronics and Communication Engineering


National Institute of Technology Calicut
Winter 202425 Notes 6

Working with Functions


You have used functions in all the programs that you have written by now. e.g.
printf . This is a pre-written function available to you. Additionally, you can
write your own functions and use it in your program. We will look at a simple
example to start o. Consider one of the most basic programs we have seen,

#i n c l u d e < s t d i o . h>

int main ( void )


{
printf ( " Programming is fun . \ n " ) ;

return 0;
}

We can write a function doing a similar job, (though trivial in this particular
case, we will still take it up to illustrate the point!),

void printMessage ( void )


{
printf ( " Programming is fun . \ n " ) ;
}

The rst line of a function denition tells the compiler the following:

1. The type of value it returns. (In this case void)

2. Its name. (printMessage - choose a name that describes the utility of


function)

3. The arguments it takes. (In this case no arguments and hence void).

The above is not yet in completely usabel form. A complete workable program
can be made the following way:

#i n c l u d e < s t d i o . h>

void printMessage ( void )


{
printf ( " Programming is fun . \ n " ) ;
}

int main ( void )

1
{
printMessage ();

return 0;
}

The above function contains two functions, printMessage and main.

ˆ Note immedeatly that we have written the printMessage function above


the main function. It does not matter which order or where we write a
function.

ˆ Program execution always begins with the main function.

ˆ Other functions are executed only when a specc call to them is made.

Reusability
#i n c l u d e < s t d i o . h>

void printMessage ( void )


{
printf ( " Programming is fun . \ n " ) ;
}

int main ( void )


{
printMessage ();
printMessage ();

return 0;
}

#i n c l u d e < s t d i o . h>

void printMessage ( void )


{
printf ( " Programming is fun . \ n " ) ;
}

int main ( void )


{
for ( i = 1; i <= 5 ; ++i )
{
printMessage ();
}

2
return 0;
}

Arguments and Local Variables


In the above example, no arguments were provided and no variables were used
inside the function. But, for any practical use of the function, we have to have
arguments and local variables.

ˆ We have already seen use of arguments with the way we used printf and
scanf.

ˆ Instead of just printing programming is fun, you could print whatever you
gave as argument using the printf function.

Lets see some more examples of programs as functions.

// Function to calculate the nth triangular number

#i n c l u d e < s t d i o . h>

void calculateTriangularNumber ( int n)


{
int i , triangularNumber = 0 ;

for ( i = 1; i <= n ; ++i )


{
t r i a n g u l a r N u m b e r += i ;
}
printf (" Triangular number %i i s %i \ n " , n, triangularNumber ) ;

int main ( void )


{
calculateTriangularNumber (10);
calculateTriangularNumber (20);
calculateTriangularNumber (50);

return 0;
}

Functions and arrays

3
// Function to find the minimum value in an array

#i n c l u d e < s t d i o . h>

int minimum ( int values [ 1 0 ] ) {

int minValue , i ; minValue = v a l u e s [ 0 ] ;


for ( i = 1; i < 1 0 ; ++i ) {
if ( v a l u e s [ i ] < minValue )
minValue = v a l u e s [ i ] ;
}
return minValue ;
}

int main ( void ) {

int scores [10] , i , minScore ;


int minimum ( int values [ 1 0 ] ) ;

printf (" Enter 10 s c o r e s \n " ) ;

for ( i = 0; i < 1 0 ; ++i )


scanf ("% i " , &s c o r e s [ i ] ) ;

m i n S c o r e = minimum ( scores );

printf ( " \ nMinimum score i s %i \ n " , minScore ) ;

return 0;

Recursive Functions
The factorial example

// Function to find a factorial recursively

#i n c l u d e < s t d i o . h>

int main ( void ) {

unsigned int j ;
unsigned long int factorial ( unsigned int n);

for ( j = 0; j < 1 1 ; ++j )

4
printf ("%2u ! = %l u \ n " , j , factorial ( j )); return 0; } // Rec

unsigned long int factorial ( unsigned int n) {

unsigned long int result ;

if ( n == 0 ) result = 1;
else
result = n * factorial (n = 1); // Notice here that we are ca

/ /How do we expand the above ?

return result ;

Questions
1. How do you create user dened libraries? Create a user dened library
for computing prime numbers. Compute prime numbers till a user input
number using this function from the newly created library.

2. Write a function which receives a oat and an int from main( ), nds the
product of these two and returns the product which is printed through
main( ).

3. A 5-digit positive integer is entered through the keyboard, write a function


to calculate sum of digits of the 5-digit number:

(a) Without using recursion

(b) Using recursion

You might also like