Lecture 5
Lecture 5
FUNCTIONS
Outline
Introduction to functions
C library functions
Function call
User defined functions
Examples
OBJECTIVES
• To learn how to design, develop and manage
large programs
• Helps in:
▫ Manageable program development (Divide and
conquer)
▫ Software reusability (abstraction)
▫ Avoid code repetition
Functions Definitions
• Every c program starts with main () function
• Additional functions are called or invoked when the program
encounters function names
• Functions could be;
• Pre-defined library functions (e.g., printf, sin, tan) or
Programmer-defined functions (e.g., my_printf, area)
• Assume that printf function was not there. How difficult your
life would be? Every time you want to print something, you
have to write several sets of instructions (that printf uses
internally) to perform this task.
▫ May take arguments
▫ May return a single value to the calling function
▫ May change the value of the function arguments (call by
reference)
Functions Definition
▫ Returning control
If nothing returned
return;
or, until reaches right brace
If something returned
return expression;
FUNCTION PROTOTYPE
▫ Function Prototype describes how a function is
called
▫ Function implementation
▫ int my_add_func(int a, int b)
▫ Function Call
▫ result = my_add_func(5, X);
▫{ Function parameters
▫… Formal parameters
▫} Actual parameter
Formal parameters must match with actual parameters
in order, number and datatype.
If the type is not the same, type conversion will be
applied
Function Prototype
• Function prototype
– Function name
– Parameters – what the function takes in
– Return type – data type function returns (default int)
– Used to validate functions
– Prototype only needed if function definition comes after use in
program
– The function with the prototype
int maximum( int, int, int );
• Takes in 3 ints
• Returns an int
• Promotion rules and conversions
– Converting to lower types can lead to errors
PRE-DEFINED FUNCTIONS
we have been using several pre-defined functions! :
EXAMPLE
//program to print the sine of an angle
#include <stdio.h>
#include <math.h>
int main(void) {
double angle;
printf (“Input angle in radians: \n“);
scanf(“%f”, &angle);
printf(“Sine of the angle is %f\n“, sin(angle));
return 0;
}
HEADER FILES
• Header files
– Contain function prototypes for library functions
– <stdlib.h> , <math.h> , etc
– Load with #include <filename>
#include <math.h>
• Custom header files
– Create file with functions
– Save as filename.h
– Load in other files with #include "filename.h"
– Reuse functions
RANDOM NUMBER GENERATION
Some pre-defined functions we have discussed
are:
main
printf
scanf
RANDOM AND PSEUDO-RANDOM NUMBERS
rand (uniform random numbers)
srand (for may be 7 random numbers)
void function() {
…
function(); // Recursive call as function calls
itself……
}
int main() {
function(); // Normal Call…
}
Recursion
• If a function keeps calling itself, stack might get
full due to number of calls and the result would
be a stack overflow.
EXAMPLES
• Example: factorials
▫ 5! = 5 * 4 * 3 * 2 * 1
▫ Notice that
5! = 5 * 4!
4! = 4 * 3! ...
▫ Can compute factorials recursively
▫ Solve base case (1! = 0! = 1) then plug in
2! = 2 * 1! = 2 * 1 = 2;
3! = 3 * 2! = 3 * 2 = 6;
int factorial(int n) {
if(n <= 1){
return 1;
}
else
return ( n * factorial(n-1) ); // Recursive call…
}
int main() {
int number;
int result;
printf(“Enter a number:\t”);
scanf(“%d”, &number);
result = factorial(number);
printf(“Factorial of %d is %d…\n”, number, result);
}
EXAMPLES 2
int fact(int k)
{ note :k!=k*(k-1)!
if (k == 0)
return 1;
else
return k*fact(k-1);
}
Example Using Recursion: The
Fibonacci Series
Sequence {f0,f1,f2,…}. First two values (f0,f1) are
1, each succeeding number is the sum of previous
two numbers.
• 1 1 2 3 5 8 13 21 34
• F(0)=1, F(1) = 1
• F(i) = F(i-1)+F(i-2)
int fibonacci(int k)
{
int term;
term = 1;
if (k>1){
term = fibonacci(k-1)+fibonacci(k-2);
}
return term;
}