4-Usage of Programming Functions
4-Usage of Programming Functions
USAGE OF
PROGRAMMING
FUNCTIONS
Malaysian Institute of Aviation Technology
printf Statement
printf(“format_control_string”, arguments);
scanf Statement
scanf(“data_type_identifier”, &arguments);
Testing of Functions
• Functional testing is a type of software testing that
evaluates the performance of individual functions of
a software application.
• The purpose of functional testing is to ensure that
the application and all of its individual functions
work as they should in the real world and meet all
requirements and specifications.
• It is a valuable testing method for verifying that the
output provided by each application function is in
line with what’s expected.
Malaysian Institute of Aviation Technology
How it Works?
• Functional testing basically the testing of the
functions of component or system is done.
• Each function should be designed, coded and tested
as a separate unit from the rest of the program.
• In order to test a function, special temporary main
functions are used to call the function using values
input from the keyboard.
• Once a function is fully tested, it can be used to test
other functions.
Malaysian Institute of Aviation Technology
How it Works?
• Functional testing is typically conducted by providing an
appropriate input to the function being tested , and then
verifying the result by comparing it to the expected
result.
• It’s typically approached from one of two perspectives:
• Requirements-focused testing - prioritizes requirements
based on risk criteria in order to evaluate the most critical and
important features and functions first.
• Business-process-focused testing - relies on knowledge of
end-user business requirements to evaluate an application’s
performance in the context of typical use cases.
Malaysian Institute of Aviation Technology
Error Evaluation
• Error is an illegal operation performed by the
user which results in abnormal working of the
program.
• Programming errors often remain undetected
until the program is compiled or executed.
• Some of the errors inhibit the program from
getting compiled or executed.
• Thus errors should be removed before compiling
and executing.
Malaysian Institute of Aviation Technology
Syntax Error
• Errors that occur when you violate the rules of writing
C/C++ syntax are known as syntax errors.
• This compiler error indicates something that must be
fixed before the code can be compiled.
• All these errors are detected by compiler and thus are
known as compile-time errors.
• Most frequent syntax errors are:
- Missing Parenthesis (}).
- Printing the value of variable without declaring it.
- Missing semicolon.
Malaysian Institute of Aviation Technology
Run-time Error
• Errors which occur during program execution (run-
time) after successful compilation are called run-
time errors.
• One of the most common run-time error is division
by zero also known as Division error.
• These types of error are hard to find as the compiler
doesn’t point to the line at which the error occurs.
Malaysian Institute of Aviation Technology
Linker Error
• These error occurs when after compilation we link
the different object files with main’s object
using Ctrl+F9 key(RUN).
• These are errors generated when the executable of
the program cannot be generated.
• This may be due to wrong function prototyping,
incorrect header files.
• One of the most common linker error is
writing Main() instead of main().
Malaysian Institute of Aviation Technology
Logical Error
• On compilation and execution of a program, desired
output is not obtained when certain input values are
given.
• These types of errors which provide incorrect output
but appears to be error free are called logical errors.
• These are one of the most common errors done by
beginners of programming.
• These errors solely depend on the logical thinking of the
programmer and are easy to detect if we follow the line
of execution and determine why the program takes that
path of execution.
Malaysian Institute of Aviation Technology
Semantic Error
• This error occurs when the statements written in the
program are not meaningful to the compiler.
Malaysian Institute of Aviation Technology
}
Malaysian Institute of Aviation Technology
EXERCISE 1
• Scanf and printf
Malaysian Institute of Aviation Technology
#include <stdio.h>
int main()
{
int age, half, years;
printf(" please enter your age: ");
scanf("%d", &age);
printf("\n\n your age you entered is %d \n", age);
half = age / 2;
years = age -5;
printf("\n\n A person half of your age would be %d years old\n", half);
printf("\n\n it has been %d years since your were 5 years old\n", years);
return 0;
}
Malaysian Institute of Aviation Technology
EXERCISE 2
• Scanf and printf
Malaysian Institute of Aviation Technology
#include <stdio.h>
int main()
{
int x;
float y;
char string[30];
char name='a';
printf("the character in memory is >> %d << \n\n\n\n", name);
char keyB[] = {'q','w', 'e', 'r', 't','y','\0'};
printf("the character in keyB memory is >> '%c' '%c' '%c' << \n\n\n\n",
keyB[0],keyB[4],keyB[2] );
char keyC[]= "qwerty";
printf("the character in memory keyC is >> '%s'<< \n\n\n\n", keyC);
char keyD[7]= {'q','w','e', 'r', 't','y'};
printf("the character in keyD memory is >> '%s'<< \n\n\n\n", keyD);
return 0; }
Malaysian Institute of Aviation Technology
EXERCISE 3
• Calculate the area of a pizza
Malaysian Institute of Aviation Technology
#include <stdio.h>
#define PI 3.14159
int main()
{
float area, circum;
int radius;
printf("What is the radius of your pizza?\n");
scanf("%d", &radius);
area = PI * (float) radius * (float)radius;
circum = 2.0 * PI * (float) radius;
printf("Your basic pizza parameters are as follows:\n");
printf("circumference = %.2f, area = %.2f\n", circum, area);
return 0;
}
Malaysian Institute of Aviation Technology
EXERCISE 4
Write a statement to accomplish each of the following:
i. Define the variables x, y, z and result to be of type float.
ii. Prompt the user to enter three integers.
iii. Read three integers from the keyboard and store them in the
variables x, y and z.
iv. Calculate X + Y / Z and assign the result to the variable
result.
v. Print the result.
Malaysian Institute of Aviation Technology
EXERCISE 5
Write a statement to accomplish each of the following:
i. Convert celcius to Fahrenheit using the following formula:
32 + (float)celcius * 9 / 5