5 Functions
5 Functions
1
Math Library Functions Available Mathematical functions
Function Header Description
• Math library functions
– perform common mathematical calculations int abs(int num) Returns the absolute value of an integer
element.
– #include <math.h>
double fabs (double num) Returns the absolute value of a double
• Format for calling functions precision element.
double pow(double x,double y) Returns x raised to the power of y.
– FunctionName ( argument );
• If multiple arguments, use comma-separated list int rand(void ) returns a random number
– y = sqrt( 900.0 );
double sin(double angle) Returns the sine of an angle the angle
• Calls function sqrt, which returns the square root of its should be in Radius.
argument
• All math functions return data type double double cos(double angle) Returns the cosine of an angle the angle
should be in Radius.
– Arguments may be constants, variables, or expressions
double sqrt(double num) Returns the sign the square root.
2
Function Definition function
Example
prototype
A function definition has the following form: • Let’s define a function to compute the cube of a number:
int cube ( int num ) {
return_type function name (formal parameter list) int result;
{
declarations result = num * num * num;
statements return result;
} }
return_type - the type of value returned by the function
• This function can be called as:
• void – indicates that the function returns nothing.
function name – any valid identifier
formal parameter list – comma separated list, describes the number n = cube(5);
and types of the arguments that get passed into the function
when its invoked.
SPRING 2003 COP3223 9 SPRING 2003 COP3223 10
3
/* An example demonstrating local variables */
#include <stdio.h> #include < stdio.h>
void print_message (int k); /*function prototype */
int main (void) void func1 (void) ; 5
{
int n;
5
int main (void)
printf(“There is a message for you.\n”); { 6
printf(“How many times do you want to see it? ”); int i = 5 ; 5
scanf(“%d”, &n); printf(“%d \n”, i);
print_message(n); func1( );
return 0; printf(“%d \n”,i);
}
return 0;
void print_message (int k) /* function definition */ }
{
int i; void func1 (void)
{
printf(“\nHere is the message.\n”); int i = 5;
for (i=0; i < k; ++i)
printf(“Have a nice day!\n”); printf(“%d \n ”, i);
} i++;
printf(“%d \n ”, i);
}
SPRING 2003 COP3223 13 SPRING 2003 COP3223 14
4
#include < stdio.h>
int min (int a, int b); Parameters
int main (void)
• A function can have zero or more parameters.
{ • In declaration header:
int j, k, m;
int f (int x, double y, char c);
printf(“Input two integers: ”);
scanf(“%d %d”, &j, &k);
the formal parameter list
m = min(j,k);
(parameter variables and their
printf(“ \nThe minimum is %d.\ n”, m);
types are declared here)
return 0 ;
}
int min(int a, int b) Input two integers: 5 6
The minimum is 5. • In function calling:
{
if (a < b) value = f(age, score, initial);
Input two integers: 11 3
return a; The mininum is 3.
else
return b; actual parameter list (cannot
} tell what their type are from
here)
SPRING 2003 COP3223 17 SPRING 2003 COP3223 18
5
1 /* Finding the maximum of three integers */
2
#include <stdio.h> 3 #include <stdio.h>
int compute_sum (int n) 4
int compute_sum (int n); 5 int maximum( int, int, int ); /* function prototype */
{ 6
int sum; 7 int main()
int main (void) 8 {
{ 9 int a, b, c;
sum = 0; 10
int n, sum; 11 printf( "Enter three integers: " );
12 scanf( "%d%d%d", &a, &b, &c );
for ( ; n > 0; --n) 13 printf( "Maximum is: %d\n", maximum( a, b, c ) );
n = 3; 14
sum += n; 15 return 0;
printf(“%d\n”, n); 16 }
printf(“%d\n”, n); 17
return sum;
sum=compute_sum(n); 18 /* Function maximum definition */
} 19 int maximum( int x, int y, int z )
printf(“%d\n”,n); 20 {
printf(“%d\n”, sum); 21 int max = x;
22
return 0; 23 if ( y > max )
} 24 max = y;
3 25
26 if ( z > max )
0 27 max = z;
3 28
29 return max;
6 30 }
Enter three integers: 22 85 17
SPRING 2003 COP3223 21 SPRING
Maximum is: 85 2003 COP3223 22
6
Correct the errors in the following Correct the errors in the following
program segments program segments
1. int g (void) { 3. void f (float a); {
printf (“Inside function g \n”);
float a;
printf (“%f”, a);
int h(void) {
}
printf(“Inside function h \n”);
}
} 4. void product (void) {
int a, b, c, result;
2. int sum(int x, int y) { printf(“Enter 3 integers: ”);
int result; scanf(“%d %d %d”, &a, &b, &c);
result = x + y; result = a * b * c;
} printf(“Result is %d\n”, result);
return result;
}