Professional Documents
Culture Documents
Lab 8 Functions
Lab 8 Functions
Lab 8 Functions
x = -2302.56;
y = fabs(x);
printf("The absolute value of %.4lf = %.4lf\n\n", x , y);
x = 232.55623;
y = sqrt(x);
printf("The square-root of %.4lf = %.4lf\n\n", x , y);
x = 232.55623;
y = log(x);
printf("The natural logarithm of %.4lf = %.4lf\n\n", x , y);
x = 23.323;
y = pow(x , 2.5);
printf("%.4lf^2.5 = %.4lf\n\n", x , y);
return 0;
}
Page 1 of 11
2. Run the Debugger on the program.
This time, instead of clicking on Next Line Button, click on Into function Button.
Wirte down your observations
Observations:
3. Modify Program1.c by using more predefined functions from math.h (e.g., sin ,
cos, ceil,….)
Save the source file as Lab8/program2.c
Note: you can find useful information about math.h function in the following page
https://www.programiz.com/c-programming/library-function/math.h
/* More functions from math.h*/
#include <stdio.h>
#include <math.h>
int main(void){
double x , y ;
x = 2.3;
y = sin(x);
printf("sin(%.2lf) = %.2lf\n\n", x , y);
x = 2.5;
y = cos(x);
printf("cosin(%.2lf) = %.2lf\n\n", x , y);
x = 5.66;
Page 2 of 11
y = ceil(x);
printf("ceil(%.2lf) = %.2lf\n\n", x , y);
x = 3.5;
y = tan(x);
printf("tan(%.2lf) = %.2lf\n\n", x , y);
return 0;
5. Remove the line #include <math.h>, recompile the program and notice the compilation
report.
Observations
Page 3 of 11
Task 2: void functions with void argument
Write Compile & Run the following C program in Dev-C++.
Save the source file as Lab8/program3.c
Compile & Run the program in Dev-C++.
Note: This is the same program of Task1.5 of Lab-6-RepetitionControlStructures - Part II.
Here, we use functions
#include<stdio.h>
void draw_Fix_stars_horizontal(void);
void draw_Fix_stars_vertical(void);
int main(void){
draw_Fix_stars_horizontal();
draw_Fix_stars_vertical();
draw_Fix_stars_horizontal();
return 0;
}
/* function draw_Fix_stars_horizontal
draws a row of 20 starts using
for loop */
void draw_Fix_stars_horizontal(void){
int j;
for(j = 1 ; j <= 20 ; j++)
printf("*");
printf("\n");
}
/* function draw_Fix_stars_vertical
uses nested for loops to draw 10 rows
of a star, followed by 18 spaces and
ended by a star */
void draw_Fix_stars_vertical(void){
int i , j ;
for(i = 1 ; i <= 10 ; i++){
printf("*");
for(j = 1 ; j <= 18 ; j++)
printf(" ");
printf("*");
printf("\n");
}
}
Page 4 of 11
1. What is the Output of the program?
void draw_Fix_stars_horizontal(void);
2) Function definition
void draw_Fix_stars_horizontal(void){
int j;
for(j = 1 ; j <= 20 ; j++)
printf("*");
printf("\n");
}
3) Function call
int main(void){
draw_Fix_stars_horizontal();
draw_Fix_stars_vertical();
draw_Fix_stars_horizontal();
return 0;
}
4) A statement of purpose of a function (See the last slide in Lecture 6).
Reuse of Function Sub-programs
Reduces the overall length of the program and the chance of error
3. Note that the definitions of the two user-defined functions are not terminated by return
statement! Why?
Page 5 of 11
4. Use the function draw_Fix_stars_horizontal()to print 20x20 stars!
Write a complete C program for this task.
Save the source file as Lab8/program4.c
#include<stdio.h>
void draw_Fix_stars_horizontal(void);
void draw_Fix_stars_vertical(void);
int main(void){
draw_Fix_stars_horizontal();
draw_Fix_stars_vertical();
draw_Fix_stars_horizontal();
return 0;
}
/* function draw_Fix_stars_horizontal
draws a row of 20 starts using
for loop */
void draw_Fix_stars_horizontal(void){
int j;
for(j = 1 ; j <= 20 ; j++)
printf("*");
printf("\n");
}
/* function draw_Fix_stars_vertical
uses nested for loops to draw 20 rows
of a star, followed by 18 spaces and
ended by a star */
void draw_Fix_stars_vertical(void){
int i , j ;
for(i = 1 ; i <= 20 ; i++){
printf("*");
for(j = 1 ; j <= 18 ; j++)
printf(" ");
printf("*");
printf("\n");
}
}
Page 6 of 11
Exercise:
Write a complete C program with four function of type void functions with void argument
as following:
1) print_right_triange: this function prints the numerical triangle we
developed in program5.c in Lab-6-RepetitionControlStructures - Part II
2) print_left_triangle: this function prints the numerical triangle we
developed in program6.c in Lab-6-RepetitionControlStructures - Part II
3) print_Pascal_triangle: this function prints the numerical triangle we
developed in program8.c in Lab-6-RepetitionControlStructures - Part II
4) instructions: this function displaying instructions to the user on how to use a
program.
The program starts by displaying instructions to the user and asks him to enter his choice.
Based on the user choice, the program prints one triangle!
As a help, you find the complete working codes for program5.c, program6.c,
program8.c at the end of this lab handout.
Save the source file as Lab8/program5.c
/* code for Lab8/program5.c */
#include<stdio.h>
void print_right_triange(void);
int main(void){
print_right_triange();
return 0;
}
void print_right_triange(void){
int i,j;
for(i=1; i<=20; i++)
{
/* Print i number of stars */
for(j=1; j<=i; j++)
{
printf("*");
}
Page 7 of 11
Task 3: void functions with input arguments
Write Compile & Run the following C program in Dev-C++.
Save the source file as Lab8/program6.c
Compile & Run the program in Dev-C++.
Note: This is the modified version of Program3.c shown above
#include<stdio.h>
void draw_stars(int n);
void draw_starSpace(int rows , int columns);
int main(void){
draw_stars(width);
draw_starSpace(height , width);
draw_stars(width);
return 0;
}
/* function draw_stars
draws a row of n starts using
for loop */
void draw_stars(int n){
int j;
for(j = 1 ; j <= n ; j++)
printf("*");
printf("\n");
}
/* function draw_starSpace
uses nested for loops to draw rows
of a star, spaces and a star*/
void draw_starSpace(int rows , int columns){
int i , j ;
for(i = 1 ; i <= rows ; i++){
printf("*");
for(j = 1 ; j <= columns-2 ; j++)
printf(" ");
printf("*");
printf("\n");
}
}
Page 8 of 11
1. What is the Output of the program?
2. What happens when the program calls a function with input arguments? Describe the
actions in the steps.
When a function call is encountering during the program execution:
1. …..
2. ….
……..
3. Modify program5.c to enable the user to choose any symbol he prefers in printing the
rectangle.
Save the source file as Lab8/program7.c
Compile & Run the program in Dev-C++.
/* code for Lab8/program7.c */
Page 9 of 11
Task 4: functions with input arguments and return
Write Compile & Run the following C program in Dev-C++.
Save the source file as Lab8/program8.c
Compile & Run the program in Dev-C++.
/* Computes n! for n greater than or equal to zero */
#include <stdio.h>
int factorial(int n);
int square(int num);
int main(void) {
int a , b ;
printf("Enters a number: ");
scanf("%d",&a);
b = factorial(a);
printf("%d! = %d\n" , a , b);
b = square (a);
printf("%^2 = %d\n" , a , b);
return 0;
}
int factorial(int n) {
int i , prod = 1 ;
/* Computes the product n * (n-1) * (n-2) * ….. * 2 * 1 */
for( i= n ; i > 1 ; i-- )
prod *= i ;
return prod ;
}
Page 10 of 11
Help Programs for The Exercise
#include<stdio.h>
int main(void){
int i , j ;
return 0;
}
#include<stdio.h>
int main(void){
int i , j ;
return 0;
}
#include<stdio.h>
int main(void){
int i , j ;
return 0;
}
Page 11 of 11