0% found this document useful (0 votes)
29 views

4-Usage of Programming Functions

Here are the statements to accomplish each task: i. float x, y, z, result; ii. printf("Enter three integers: "); iii. scanf("%f %f %f", &x, &y, &z); This defines the variables x, y, z and result as floats. It then prompts the user to enter three integers and uses scanf to read three float values into the variables x, y, and z.

Uploaded by

Idham Sazali
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views

4-Usage of Programming Functions

Here are the statements to accomplish each task: i. float x, y, z, result; ii. printf("Enter three integers: "); iii. scanf("%f %f %f", &x, &y, &z); This defines the variables x, y, z and result as floats. It then prompts the user to enter three integers and uses scanf to read three float values into the variables x, y, and z.

Uploaded by

Idham Sazali
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 29

Malaysian Institute of Aviation Technology

USAGE OF
PROGRAMMING
FUNCTIONS
Malaysian Institute of Aviation Technology

What is Programming Functions?


• A section of a program that performs a specific
task.
• A function is a type of procedure or routine,
which returns a value.
• Most programming languages come with a
prewritten set of functions that are kept in
a library.
• Can write our own functions to perform
specialized tasks.
Malaysian Institute of Aviation Technology

Input Output Devices


• Input devices are used to perform the input
operation.
– Eg: keyboard, mouse, microphone, scanner.
• Output devices are used to perform the output
operation.
– Eg: monitor, printer, speaker.
Malaysian Institute of Aviation Technology

Input Output Operation


• Input operation in basic C programming is to get
input data from the keyboard using scanf statement.
• While output operation is to display the data at the
monitor screen using printf statement.
Malaysian Institute of Aviation Technology

printf Statement
printf(“format_control_string”, arguments);

❑ Describe output formats

o List of variable names received by the function from the caller –


arguments (data input)
o parameter_list : (parameter_type parameter name)
o parameter_type: int ,char, float, double
o If function does not has any parameter_list : (void) or ()
Malaysian Institute of Aviation Technology

scanf Statement
scanf(“data_type_identifier”, &arguments);

❑ Describe output formats


❑ Eg: %d for integer input, %c for
character input etc.

o List of variable names received by the function from the caller –


arguments (data input)
Malaysian Institute of Aviation Technology

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

Common Error of Functions


The most common errors can be classified as
follows:
• Syntax Error
• Run-time Error
• Linker Error
• Logical Error
• Semantic Error
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

Syntax Error Example


// C program to illustrate
// syntax error
#include<stdio.h>
void main() semicolon missed
{
int x = 10;
int y = 15;

printf("%d", (x, y))


}
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

Run-time Error Example


// C program to illustrate run-time error
#include<stdio.h>
void main() • Wrong logic
{ • Number is divided by 0
int n = 9, div = 0; • Abnormally terminated
div = n/0;

printf("resut = %d", div);


}
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

Linker Error Example


// C program to illustrate linker error
#include<stdio.h>
• Main() should be main()
void Main()
{
int a = 10;
printf("%d", a);
}
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

Logical Error Example


// C program to illustrate logical error
int main()
{
int i = 0; • Semicolon after for loop
for(i = 0; i < 3; i++);
{
printf("loop ");
continue;
}
return 0;
}
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

Semantic Error Example


// C program to illustrate semantic error
void main()
{
• a + b = c; no value is
assigned to a and b
int a, b, c;
a + b = c; //semantic error

}
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

You might also like