CS1001 - Programming in C
First year Course
Dr. Sakthi Balan Muthiah
Professor, CSE
Functions and Program Structure
Sakthi Balan Muthiah
Functions
• Functions are invoked by function calls which
• Functions are used to modularise the speci es the function name and arguments
programs so that some speci c task can
be done by them • The arguments are used to execute the tasks
given in the function.
• C standard library provides a rich • This is in perfect analogy with a hierarchical form
collection of functions - math calculations, of management
string manipulations, char manipulations,
input / output and other tasks • A Boss function can call its subordinate and who
intern can call its subordinate and so on to carry
• User-de ned functions: Functions out di erent tasks
de ned by users to perform speci c tasks
• The statements in the function can be
hidden from other function. This type of
hiding is crucial to good software
engineering
Sakthi Balan Muthiah
fi
fi
ff
fi
fi
fi
Math Library Functions
• sqrt(x) - square root of x • fmod(x,y) - remainder of x/y as a oating-point
• cbrt(x) - cube root of x (C99 and C11) number
• exp(x) - exponential function • sin(x), cos(x) and tan(x) - trigonometric functions
• log(x) - natural logarithm of x (base e)
• log10(x) - log of x with base 10
• fabs(x) - absolute value of x where x is a oating
number include math.h when you use math library
functions
• ceil(x) - x is rounded o to the smallest integer y
which is great than x
• oor(x) - x is rounded o to the largest integer y
which is less than x
• pow(x,y) - x raised to y
Sakthi Balan Muthiah
fl
ff
ff
fl
fl
Modularising or Functionalising
• Modularising or functionalising has real bene ts:
• Makes the program development more manageable
• Building new programs by using existing functions (software reusability)
• Redundant codes can be minimised
• This approach is called divide-and-conquer
Sakthi Balan Muthiah
fi
Function Choose meaningful function names and
Example meaningful parameter names Programs
should be written as collections of small
#include <stdio.h>
functions.
int square(int number); // function prototype Programs will be easier to write, debug,
int main(void) { maintain, modify and reuse
// loop 10 times and calculate and output square of x each time
for (int x = 1; x <= 10; ++x) { • Variables de ned in functions are
printf("%d ", square(x)); // function call called local variables.
} • They can be accessed only from
puts(""); inside the function.
} • Arguments are the parameters
// square function definition returns the square of its parameter
through which the calling and the
called function can
int square(int number) { // number is a copy of the function's argument
communicate.
return number * number; // returns square of number as an int
• Parameters are also local
}
variables.
Sakthi Balan Muthiah
fi
Function
int square(int number); When the function call occurs the compiler checks the following:
1. Number of arguments in the prototype and in the called
is called a function function is the same
prototype 2. Arguments of correct types
• int inside the brackets tells 3. Arguments order are the same
that there is one argument 4. The return value is consistent with the data type defined in
which is of integer type the prototype
• int outside tells that the Format of a function definition
function returns a value that is
of integer type return-value-type function-name(parameter-list) {
statements
• semi-colon at the end is
necessary }
Sakthi Balan Muthiah
Function
Format of a function definition
return-value-type function-name(parameter-list) {
statements
When the called function finishes the task it switches
the control to the called function with a return value
of the datatype it is defined for. This is done by
return; \\ if there is no return
return expression; \\ if there is return value involved
Sakthi Balan Muthiah
Valid One
int maximum(int, int, int);
Argument Coercion Note
printf("%.3f\n", sqrt(4)); The statement
return 0;
Note
in the main( ) indicates
The compiler uses function if the program
prototypes to validate function executed correctly or
calls. not. ‘0’ indicates there
was no error.
Sakthi Balan Muthiah
Max of three integers using
function
Sakthi Balan Muthiah
Mixed-Type Expressions
• Expressions containing di erent datatypes are called Mixed-Type expressions
• Compiler converts the datatype of all the variables to the “highest” type in the expression
(called as promotion)
• For example
• if one value is long double then other values are converted to long double
• If one is double then all other values are converted to double
• If one is float then others are converted to float
• Value can be converted to a lower type only by explicitly assigning to a variable de ned to be of
that type (normally this is not advised as the value changes)
• For example, square(4.5) will return 16, but not 20.25 or even 20 !
• Many compilers issue warnings for the above
Sakthi Balan Muthiah
ff
fi
Design a simple calculator program that gets input of two
positive integers m and n and then calculates according to
the user’s input either m+n, m*n, m-n, or m/n
Use switch statement to select the operation and design
functions to perform each of the four operations.
Sakthi Balan Muthiah