FUNCTION
Introduction to
Function
A function is a subroutine that contains one or more
executable statements. A function should perform only one task. Each function has a name and a list of
arguments that it will receive. A function may be given any name, with the
exception of main. No function can have the same as a reserved word.
? The main( ) function is
always the first to execute. It doesn’t need to be first in a program, but is
usually is. Good C programmer write programs that consists of many small
function, even if their programs execute one or more of these functions only
once.
Creating a function.
Function basic:
1. Function
must have name.
2. Function
name - same rule that applies to naming variables.
3. Function
names have one set of parentheses immediately following them ().
4. The body of the function, starting immediately after the
closing parentheses of the functions name
FORMAT:
Explanations:
return-value-type The
data type of resulted from the function caller.
The return-value-type void indicates that a function does not
return a value.
Unspecified return-value-type is assumed by the compiler
to be int.
function-name Any valid identifier.
parameter list A
list of variable declarations that specify each parameter’s name and type.
FORMAT:
type var_1, type
var_2,.... , type var_N
If a function has no
parameters, that is when the parameter list is empty or void, we use either
function-name (void)
FUNCTION STRUCTURE :
Preprocessor directive
return_type
function_name(parameter ); // function prototype / function
declaration
void main()
{ variables
declaration;
statements;
function_name(parameter); // function call
……….
............
}
return_type function_name(parameter) // function header
{ variables
declaration;
statements;
// function definition
…………….
return value;
}
CODING EXAMPLE (BASIC) :
#include <stdio.h>
Function
Prototype/Declarations
void
Welcome(void);
void main()
Function
Call
{
Welcome();
}
Function
Header
void Welcome(void)
{
Function
Definition
printf(“Welcome to Malaysia\n”);
}
Four types of function :
- Function
without return value and without passing parameter
Example :
1.
void mainMenu(void);
2.
void mainMenu( );
- Function
without return value but with passing parameter(s)
Example :
1.
void displayResult(int total);
2.
void display(int num, float total);
- Function
with return value but without passing parameter
Example :
1.
int GetInput( );
2.
float GetValue(void);
- Function
with return value and with passing parameter(s)
Example :
1.
int areaCircle(int radius);
2.
float average(float num1, float num2);
EXAMPLE 1(a)
// Program that demonstrate
the use of function
// Function without return
value and without passing parameter
#include <stdio.h>
// Function declaration or
function prototype
void nextFun(void);
void thirdFun(void);
// The following program
illustrates function calls
void main()
{
printf("First function called main()\n");
nextFun(); //
second function is called
thirdFun(); //
third function is called
nextFun(); //
second function is called
thirdFun(); //
third function is called
nextFun(); //
second function is called
thirdFun(); // third function is called
printf("main( ) is completed\n");
}
// Function definition
void nextFun(void) // second function
declaration
{
printf("Inside nextFun()\n"); // no variables are defined // in the program
}
// Function definition
void thirdFun(void) // third function
declaration
{
printf("Inside thirdFun()\n");
}
EXAMPLE 1(b)
// Program that demonstrate
the use of external variable
//Function without return
value and without passing parameter(s) - global variable
#include <stdio.h>
// Function declaration or
function prototypes
void check(void);
// External variables
int numlike;
// Main function
void main(void)
{
printf("Please enter any number you like:");
scanf("%d",&numlike);
check(); //
Function call
}
// Checks if number is lucky
number
// Function definition
void check(void)
{
if (numlike==10)
{
printf("Great, you are selected as best number
holder\n");
}
else
{
printf("Sorry, you failed.
Try again\n");
}
}
EXAMPLE
2(a)
//Program that demonstrate
the use of internal variable & passing parameter
//Function
without return value but with passing parameter(s)
#include <stdio.h>
// Function declaration or
function prototypes
void check(int numlike);
// Main function
void main(void)
{
// Local variables
int numlike;
printf("Please enter any number you like:");
scanf("%d",&numlike);
check(numlike); //
Function call
}
// Checks if number is lucky number
// Function definition
void check(int numlike)
{
if (numlike==10)
{
printf("Great, you are selected as best number
holder\n");
}
else
{
printf("Sorry, you failed.
Try again\n");
}
}
EXAMPLE
2(b)
// Program that demonstrate
the use of functions returning integer value
//Function without return
value but with passing parameter(s)
#include <stdio.h>
// Function declaration or
prototype
void multiply(int num1, int
num2);
void main()
{
int num1, num2;
printf("Please enter first number :");
scanf("%d",&num1);
printf("Please enter second number : ");
scanf("%d",&num2);
multiply(num1,num2);
}
// Function definition
void multiply (int num1, int
num2)
{
int ans;
ans = num1*num2;
printf("The answer is %d\n",ans);
}
EXAMPLE
2(c)
// Program that demonstrate
the use of functions returning floating point value
//Function without return
value but with passing parameter(s)
#include <stdio.h>
//Function declaration @
prototype
void sum(float a, float
b);
// Main program
void main()
{
float first, second;
first = 1023.23;
second = 990.9;
sum(first,second);
}
// Function definition
void sum(float a, float b)
{
float ans;
ans = a+ b;
printf("The answer is %.2f\n",ans);
}
EXAMPLE
3
//Function with return value
but without passing parameter
#include <stdio.h>
// Function declaration or
function prototypes
int get_input();
// Main function
void main()
{
int input;
input = get_input();
// Function call
printf("Number entered is %d\n",input);
}
// Function definition (to
get input)
int get_input()
{
int numlike;
printf("Please enter any number you like :");
scanf("%d",&numlike);
return (numlike);
}
EXAMPLE
4
//Function with return value
and with passing parameter
#include <stdio.h>
// Function declaration or
function prototypes
int calculate(int numlike);
// Main function
void main()
{
int numlike, answer;
printf("Please enter any number you like :");
scanf("%d",&numlike);
answer = calculate(numlike); // Function call
printf("Answer :%d\n",answer);
}
// Function definition (to
calculate)
int calculate(int numlike)
{
int ans;
ans = numlike * numlike;
return (ans);
}
EXAMPLE 5
/* Program that demonstrate
the use of function and parameter passing */
#include <stdio.h>
// Function declaration or
function prototypes
int getInput();
void displayInput(int
input);
void checkInput(int input);
// Main function
main()
{
int input;
input = getInput();
// Function call
displayInput(input); // Function call
checkInput(input);
// Function call
return 0;
}
// Function definition (to
get input)
int getInput()
{
int numlike;
printf("Please enter any number you like :");
scanf("%d",&numlike);
return(numlike);
}
// Function definition (to
display input)
void displayInput(int input)
{
printf("The number you like is %d\n",input);
}
// Function definition (to
checks if number is lucky number)
void checkInput(int input)
{
if (input==10)
printf("Great, you are
selected as best number holder\n");
else
printf("Sorry, you failed.
Try again\n");
}
Math Library Function
Math library function allows the programmer to perform
certain common mathematical calculations.
Functions are normally called by writing the name of the function
followed by the argument (or a comma-separated list of arguments) of the
function followed by a right parenthesis.
For example, a programmer desiring to calculate and
print the square root of 9.00 using Math
Library Function might write
printf(“Square root of
9 is %d", sqrt(9) );
When this statement is executed, the math library function sqrt
is called to calculate the square root of the number contained in
the parenthesis (9). The number 9
is the argument of the sqrt function.
Table shows
commonly used math library functions.
Function
Description
Example
ceil ( x )
round x
to the smallest integer not less than x
ceil( 9.2 ) is 10.0
ceil( - 9.8 ) is
- 9.0
cos ( x )
trigonometric cosine of x
(x in radians)
cos ( 0. 0 ) is 1.0
exp ( x )
exponential function ex
exp ( 1.0 ) is 2.71828
exp ( 2.0 ) is 7.38906
fabs ( x )
absolute value of x
if x > 0 then abs ( x ) is x
if x = 0 then abs ( x ) is 0
if x < 0 then abs ( x ) is x
floor ( x )
rounds x to the largest integer not greater than x
floor ( 9.2 ) is 9.0
floor ( - 9.8 ) is - 10.0
fmod ( x , y )
reminder of x / y as a floating point number
fmod ( 13.657 , 2.333 ) is
1.992
log ( x )
natural logarithm if x (base e)
log ( 2.718282 ) is 1.0
log ( 7.389056 ) is 2.0
log10 ( x )
logarithm of x (base 10)
log ( 10.0 ) is 1.0
log ( 100.0 ) is 2.0
pow ( x , y)
X raised to power y ( x y )
pow ( 2 , 7) is 128
pow ( 9 , .5 ) is 3
sin ( x )
trigonometric sine of x
( x in radians )
sin ( 0.0 ) is 0
sqrt ( x )
square root of x
sqrt ( 900.0 ) is 30.0
sqrt ( 9.0 ) is 3.0
tan ( x )
trigonometric tangent of x
( x in radians )
tan ( 0.0 ) is 0
EXAMPLE 1 :
Math Library Function
printf(“Power of 92 is %.2f:", pow(9,2));
EXAMPLE 2
: Function
//Program that demonstrates uses a function with
arguments to calculate sqrt
#include <stdio.h>
// Function declaration or function prototype
void power(float num1);
// Main function
void main()
{
float
num1;
printf("Please
enter any number you like:");
scanf("%f",&num1);
power(num1); // Function call power() with passing value
}
// Function definition
void power(float num1)
{
printf("Power
of %.2f is %.2f\n",num1,num1*num1);
}
Function exercise
Exercise 1
Write a C program which consist of the following function prototype(declaration).
a. void displayHeader();
a. float getFloat();
c. void displayFloat(float aFloat);
d. void displayFooter();
From main function your have to call the function accodingly.
see the output? you may download this sample program from here
Exercise 2
Write a program to find the power 2 of an integer.
The program consists of the following function prototype.
a. void displayHeader();
b. int getInteger();
c. int power2(int aNum);
d. void displayResult(int num, int result);
e. void displayFooter();
From main function your have to call the function accodingly.
see the output? you may download this program from here
Exercise 3
Write a C program to convert mark to gpa and mark to grade.
This program consist 5 function.
The conversion table is given below:
GRADE GPA MARKS
A 4.00 80-100
B 3.67 75-79
C 3.33 70-74
D 3.00 65-69
E 2.67 60-64
F 2.33 55-59
G 2.00 50-54
H 1.67 45-49
I 1.00 40-44
z 0.00 0-39
a. funtion to display Introduction of the program(no passing paramteter and no return value).
b. function to get mark from user ( this function will ask user to enter mark then return the mark).
c. function to convert mark to gpa.( this function will get mark as a paramater and return the gpa)
d. function to convert mark to grade.( this function will get mark as a paramater and return the grade)
e. funtion to display mark, gpa and grade (this funtion will have 3 passing parameter e.g mark,gpa and grade).
From main function your have to call the function accodingly.
see the output? you may download this program from here
Exercise 4
Write a C program to construct a mini calculator system.
This function consist of 7 functions.
a. function header as intorduction of the system
b. function footer to display a copyright.
c. function getInteger.
(this function will ask user to enter an integer then return the value)
d. function add .
( this function will have 2 passing parameter , do addition operation and return the result)
e. function sub .
( this function will have 2 passing parameter , do subtract operation and return the result)
f. function mul .
( this function will have 2 passing parameter , do multiply operation and return the result)
g. function dev .
( this function will have 2 passing parameter , do devide operation and return the result)
From main function your have to call the function accodingly.
Exercise 5
Write a c program to calculate cgpa for a student
( refer to the Exercise 3)
Using function ask user to enter no of subject.
Exercise 6
Write a C program of Mark Conversion System. The program will convert from mark to grade point or from mark to grade for any mark entered by user.
The conversion table is given below:
GRADE GPA MARKS
A 4.00 80-100
B 3.67 75-79
C 3.33 70-74
D 3.00 65-69
E 2.67 60-64
F 2.33 55-59
G 2.00 50-54
H 1.67 45-49
I 1.00 40-44
z 0.00 0-39
Note:
You are required to use AT LEAST 4 FUNCTIONS statements when writing the program.
Write a menu driven program which has the following options:
a. Mark to Grade Point (GPA) Conversion.
b. Mark to Grade (A, B ,C to Z ) Conversion.
o. Exit
The interface of the system should be as follows:
Mark Conversion System for British Malaysian Institute Sdn. Bhd.
-------------------------------------------------------------------------------
a. Mark to Grade Point (GPA) Conversion
b. Mark to Grade (A, B, C to Z ) Conversion
o. Exit
------------------------------------------------------------------------------
Please enter your choice :
If .a. is entered:
-------------------------------------------------------------------------------
Mark Conversion System for British Malaysian Institute Sdn. Bhd
Mark to Grade point (GPA)
--------------------------------------------------------------------------------
Please enter the mark: 80.0
For the mark= 80.0 the GPA is 4.00
---------------------------------------------------------------------------------
then the program shall return to the menu
If .b. is entered:
-------------------------------------------------------------------------------
Mark Conversion System for British Malaysian Institute Sdn. Bhd
Mark to Grade ( A, B, C to Z)
--------------------------------------------------------------------------------
Please enter the mark: 80.0
For the mark= 80.0 the Grade is A
---------------------------------------------------------------------------------
then the program shall return to the menu
The program MUST check whether the value of unit input by the user is positive, zero or negative.
Error message MUST be displayed if zero or negative values are typed in.
If .o. is entered, the program shall end.
Otherwise, the program shall return to the first screen by mentioning wrong choice is entered.
Name your file as group_mark_conversion_your_name_function.c (e.g. L01_mark_conversion_yosofkadase_function.c)
and Submit your work by upload your file here: http://yosof.net
FUNCTION
Introduction to
Function
A function is a subroutine that contains one or more
executable statements. A function should perform only one task. Each function has a name and a list of
arguments that it will receive. A function may be given any name, with the
exception of main. No function can have the same as a reserved word.
? The main( ) function is
always the first to execute. It doesn’t need to be first in a program, but is
usually is. Good C programmer write programs that consists of many small
function, even if their programs execute one or more of these functions only
once.
Creating a function.
Function basic:
1. Function
must have name.
2. Function
name - same rule that applies to naming variables.
3. Function
names have one set of parentheses immediately following them ().
4. The body of the function, starting immediately after the
closing parentheses of the functions name
FORMAT:
Explanations:
return-value-type The
data type of resulted from the function caller.
The return-value-type void indicates that a function does not
return a value.
Unspecified return-value-type is assumed by the compiler
to be int.
function-name Any valid identifier.
parameter list A
list of variable declarations that specify each parameter’s name and type.
FORMAT:
type var_1, type
var_2,.... , type var_N
If a function has no
parameters, that is when the parameter list is empty or void, we use either
function-name (void)
FUNCTION STRUCTURE :
Preprocessor directive
return_type
function_name(parameter ); // function prototype / function
declaration
void main()
{ variables
declaration;
statements;
function_name(parameter); // function call
……….
............
}
return_type function_name(parameter) // function header
{ variables
declaration;
statements;
// function definition
…………….
return value;
}
CODING EXAMPLE (BASIC) :
#include <stdio.h>
Function
Prototype/Declarations
void
Welcome(void);
void main()
Function
Call
{
Welcome();
}
Function
Header
void Welcome(void)
{
Function
Definition
printf(“Welcome to Malaysia\n”);
}
Four types of function :
- Function
without return value and without passing parameter
Example :
1.
void mainMenu(void);
2.
void mainMenu( );
- Function
without return value but with passing parameter(s)
Example :
1.
void displayResult(int total);
2.
void display(int num, float total);
- Function
with return value but without passing parameter
Example :
1.
int GetInput( );
2.
float GetValue(void);
- Function
with return value and with passing parameter(s)
Example :
1.
int areaCircle(int radius);
2.
float average(float num1, float num2);
EXAMPLE 1(a)
// Program that demonstrate
the use of function
// Function without return
value and without passing parameter
#include <stdio.h>
// Function declaration or
function prototype
void nextFun(void);
void thirdFun(void);
// The following program
illustrates function calls
void main()
{
printf("First function called main()\n");
nextFun(); //
second function is called
thirdFun(); //
third function is called
nextFun(); //
second function is called
thirdFun(); //
third function is called
nextFun(); //
second function is called
thirdFun(); // third function is called
printf("main( ) is completed\n");
}
// Function definition
void nextFun(void) // second function
declaration
{
printf("Inside nextFun()\n"); // no variables are defined // in the program
}
// Function definition
void thirdFun(void) // third function
declaration
{
printf("Inside thirdFun()\n");
}
EXAMPLE 1(b)
// Program that demonstrate
the use of external variable
//Function without return
value and without passing parameter(s) - global variable
#include <stdio.h>
// Function declaration or
function prototypes
void check(void);
// External variables
int numlike;
// Main function
void main(void)
{
printf("Please enter any number you like:");
scanf("%d",&numlike);
check(); //
Function call
}
// Checks if number is lucky
number
// Function definition
void check(void)
{
if (numlike==10)
{
printf("Great, you are selected as best number
holder\n");
}
else
{
printf("Sorry, you failed.
Try again\n");
}
}
EXAMPLE
2(a)
//Program that demonstrate
the use of internal variable & passing parameter
//Function
without return value but with passing parameter(s)
#include <stdio.h>
// Function declaration or
function prototypes
void check(int numlike);
// Main function
void main(void)
{
// Local variables
int numlike;
printf("Please enter any number you like:");
scanf("%d",&numlike);
check(numlike); //
Function call
}
// Checks if number is lucky number
// Function definition
void check(int numlike)
{
if (numlike==10)
{
printf("Great, you are selected as best number
holder\n");
}
else
{
printf("Sorry, you failed.
Try again\n");
}
}
EXAMPLE
2(b)
// Program that demonstrate
the use of functions returning integer value
//Function without return
value but with passing parameter(s)
#include <stdio.h>
// Function declaration or
prototype
void multiply(int num1, int
num2);
void main()
{
int num1, num2;
printf("Please enter first number :");
scanf("%d",&num1);
printf("Please enter second number : ");
scanf("%d",&num2);
multiply(num1,num2);
}
// Function definition
void multiply (int num1, int
num2)
{
int ans;
ans = num1*num2;
printf("The answer is %d\n",ans);
}
EXAMPLE
2(c)
// Program that demonstrate
the use of functions returning floating point value
//Function without return
value but with passing parameter(s)
#include <stdio.h>
//Function declaration @
prototype
void sum(float a, float
b);
// Main program
void main()
{
float first, second;
first = 1023.23;
second = 990.9;
sum(first,second);
}
// Function definition
void sum(float a, float b)
{
float ans;
ans = a+ b;
printf("The answer is %.2f\n",ans);
}
FUNCTION
Introduction to
Function
A function is a subroutine that contains one or more
executable statements. A function should perform only one task. Each function has a name and a list of
arguments that it will receive. A function may be given any name, with the
exception of main. No function can have the same as a reserved word.
? The main( ) function is
always the first to execute. It doesn’t need to be first in a program, but is
usually is. Good C programmer write programs that consists of many small
function, even if their programs execute one or more of these functions only
once.
Creating a function.
Function basic:
1. Function
must have name.
2. Function
name - same rule that applies to naming variables.
3. Function
names have one set of parentheses immediately following them ().
4. The body of the function, starting immediately after the
closing parentheses of the functions name
FORMAT:
Explanations:
return-value-type The
data type of resulted from the function caller.
The return-value-type void indicates that a function does not
return a value.
Unspecified return-value-type is assumed by the compiler
to be int.
function-name Any valid identifier.
parameter list A
list of variable declarations that specify each parameter’s name and type.
FORMAT:
type var_1, type
var_2,.... , type var_N
If a function has no
parameters, that is when the parameter list is empty or void, we use either
function-name (void)
FUNCTION STRUCTURE :
Preprocessor directive
return_type
function_name(parameter ); // function prototype / function
declaration
void main()
{ variables
declaration;
statements;
function_name(parameter); // function call
……….
............
}
return_type function_name(parameter) // function header
{ variables
declaration;
statements;
// function definition
…………….
return value;
}
}
CODING EXAMPLE (BASIC) :
#include <stdio.h>
|
void
Welcome(void);
void main()
|
Welcome();
}
|
void Welcome(void)
{
|
}
Four types of function :
|
EXAMPLE 1(a)
// Program that demonstrate
the use of function
// Function without return
value and without passing parameter
#include <stdio.h>
// Function declaration or
function prototype
void nextFun(void);
void thirdFun(void);
// The following program
illustrates function calls
void main()
{
printf("First function called main()\n");
nextFun(); //
second function is called
thirdFun(); //
third function is called
nextFun(); //
second function is called
thirdFun(); //
third function is called
nextFun(); //
second function is called
thirdFun(); // third function is called
printf("main( ) is completed\n");
}
// Function definition
void nextFun(void) // second function
declaration
{
printf("Inside nextFun()\n"); // no variables are defined // in the program
}
// Function definition
void thirdFun(void) // third function
declaration
{
printf("Inside thirdFun()\n");
}
EXAMPLE 1(b)
// Program that demonstrate
the use of external variable
//Function without return
value and without passing parameter(s) - global variable
#include <stdio.h>
// Function declaration or
function prototypes
void check(void);
// External variables
int numlike;
// Main function
void main(void)
{
printf("Please enter any number you like:");
scanf("%d",&numlike);
check(); //
Function call
}
// Checks if number is lucky
number
// Function definition
void check(void)
{
if (numlike==10)
{
printf("Great, you are selected as best number
holder\n");
}
else
{
printf("Sorry, you failed.
Try again\n");
}
}
EXAMPLE
2(a)
//Program that demonstrate
the use of internal variable & passing parameter
//Function
without return value but with passing parameter(s)
#include <stdio.h>
// Function declaration or
function prototypes
void check(int numlike);
// Main function
void main(void)
{
// Local variables
int numlike;
printf("Please enter any number you like:");
scanf("%d",&numlike);
check(numlike); //
Function call
}
// Checks if number is lucky number
// Function definition
void check(int numlike)
{
if (numlike==10)
{
printf("Great, you are selected as best number
holder\n");
}
else
{
printf("Sorry, you failed.
Try again\n");
}
}
EXAMPLE
2(b)
// Program that demonstrate
the use of functions returning integer value
//Function without return
value but with passing parameter(s)
#include <stdio.h>
// Function declaration or
prototype
void multiply(int num1, int
num2);
void main()
{
int num1, num2;
printf("Please enter first number :");
scanf("%d",&num1);
printf("Please enter second number : ");
scanf("%d",&num2);
multiply(num1,num2);
}
// Function definition
void multiply (int num1, int
num2)
{
int ans;
ans = num1*num2;
printf("The answer is %d\n",ans);
}
EXAMPLE
2(c)
// Program that demonstrate
the use of functions returning floating point value
//Function without return
value but with passing parameter(s)
#include <stdio.h>
//Function declaration @
prototype
void sum(float a, float
b);
// Main program
void main()
{
float first, second;
first = 1023.23;
second = 990.9;
sum(first,second);
}
// Function definition
void sum(float a, float b)
{
float ans;
ans = a+ b;
printf("The answer is %.2f\n",ans);
}
EXAMPLE
3
//Function with return value
but without passing parameter
#include <stdio.h>
// Function declaration or
function prototypes
int get_input();
// Main function
void main()
{
int input;
input = get_input();
// Function call
printf("Number entered is %d\n",input);
}
// Function definition (to
get input)
int get_input()
{
int numlike;
printf("Please enter any number you like :");
scanf("%d",&numlike);
return (numlike);
}
EXAMPLE
4
//Function with return value
and with passing parameter
#include <stdio.h>
// Function declaration or
function prototypes
int calculate(int numlike);
// Main function
void main()
{
int numlike, answer;
printf("Please enter any number you like :");
scanf("%d",&numlike);
answer = calculate(numlike); // Function call
printf("Answer :%d\n",answer);
}
// Function definition (to
calculate)
int calculate(int numlike)
{
int ans;
ans = numlike * numlike;
return (ans);
}
EXAMPLE 5
/* Program that demonstrate
the use of function and parameter passing */
#include <stdio.h>
// Function declaration or
function prototypes
int getInput();
void displayInput(int
input);
void checkInput(int input);
// Main function
main()
{
int input;
input = getInput();
// Function call
displayInput(input); // Function call
checkInput(input);
// Function call
return 0;
}
// Function definition (to
get input)
int getInput()
{
int numlike;
printf("Please enter any number you like :");
scanf("%d",&numlike);
return(numlike);
}
// Function definition (to
display input)
void displayInput(int input)
{
printf("The number you like is %d\n",input);
}
// Function definition (to
checks if number is lucky number)
void checkInput(int input)
{
if (input==10)
printf("Great, you are
selected as best number holder\n");
else
printf("Sorry, you failed.
Try again\n");
}
Math Library Function
Math library function allows the programmer to perform
certain common mathematical calculations.
Functions are normally called by writing the name of the function
followed by the argument (or a comma-separated list of arguments) of the
function followed by a right parenthesis.
For example, a programmer desiring to calculate and
print the square root of 9.00 using Math
Library Function might write
printf(“Square root of
9 is %d", sqrt(9) );
When this statement is executed, the math library function sqrt
is called to calculate the square root of the number contained in
the parenthesis (9). The number 9
is the argument of the sqrt function.
Table shows
commonly used math library functions.
Function
|
Description
|
Example
|
ceil ( x )
|
round x
to the smallest integer not less than x
|
ceil( 9.2 ) is 10.0
ceil( - 9.8 ) is
- 9.0
|
cos ( x )
|
trigonometric cosine of x
(x in radians)
|
cos ( 0. 0 ) is 1.0
|
exp ( x )
|
exponential function ex
|
exp ( 1.0 ) is 2.71828
exp ( 2.0 ) is 7.38906
|
fabs ( x )
|
absolute value of x
|
if x > 0 then abs ( x ) is x
if x = 0 then abs ( x ) is 0
if x < 0 then abs ( x ) is x
|
floor ( x )
|
rounds x to the largest integer not greater than x
|
floor ( 9.2 ) is 9.0
floor ( - 9.8 ) is - 10.0
|
fmod ( x , y )
|
reminder of x / y as a floating point number
|
fmod ( 13.657 , 2.333 ) is
1.992
|
log ( x )
|
natural logarithm if x (base e)
|
log ( 2.718282 ) is 1.0
log ( 7.389056 ) is 2.0
|
log10 ( x )
|
logarithm of x (base 10)
|
log ( 10.0 ) is 1.0
log ( 100.0 ) is 2.0
|
pow ( x , y)
|
X raised to power y ( x y )
|
pow ( 2 , 7) is 128
pow ( 9 , .5 ) is 3
|
sin ( x )
|
trigonometric sine of x
( x in radians )
|
sin ( 0.0 ) is 0
|
sqrt ( x )
|
square root of x
|
sqrt ( 900.0 ) is 30.0
sqrt ( 9.0 ) is 3.0
|
tan ( x )
|
trigonometric tangent of x
( x in radians )
|
tan ( 0.0 ) is 0
|
EXAMPLE 1 :
Math Library Function
printf(“Power of 92 is %.2f:", pow(9,2));
EXAMPLE 2
: Function
//Program that demonstrates uses a function with
arguments to calculate sqrt
#include <stdio.h>
// Function declaration or function prototype
void power(float num1);
// Main function
void main()
{
float
num1;
printf("Please
enter any number you like:");
scanf("%f",&num1);
power(num1); // Function call power() with passing value
}
// Function definition
void power(float num1)
{
printf("Power
of %.2f is %.2f\n",num1,num1*num1);
}
Function exercise
Exercise 1
Write a C program which consist of the following function prototype(declaration).
a. void displayHeader();
a. float getFloat();
c. void displayFloat(float aFloat);
d. void displayFooter();
From main function your have to call the function accodingly.
see the output? you may download this sample program from here
Exercise 2
Write a program to find the power 2 of an integer.
The program consists of the following function prototype.
a. void displayHeader();
b. int getInteger();
c. int power2(int aNum);
d. void displayResult(int num, int result);
e. void displayFooter();
From main function your have to call the function accodingly.
see the output? you may download this program from here
Exercise 3
Write a C program to convert mark to gpa and mark to grade.
This program consist 5 function.
The conversion table is given below:
GRADE GPA MARKS
A 4.00 80-100
B 3.67 75-79
C 3.33 70-74
D 3.00 65-69
E 2.67 60-64
F 2.33 55-59
G 2.00 50-54
H 1.67 45-49
I 1.00 40-44
z 0.00 0-39
a. funtion to display Introduction of the program(no passing paramteter and no return value).
b. function to get mark from user ( this function will ask user to enter mark then return the mark).
c. function to convert mark to gpa.( this function will get mark as a paramater and return the gpa)
d. function to convert mark to grade.( this function will get mark as a paramater and return the grade)
e. funtion to display mark, gpa and grade (this funtion will have 3 passing parameter e.g mark,gpa and grade).
From main function your have to call the function accodingly.
see the output? you may download this program from here
Exercise 4
Write a C program to construct a mini calculator system.
This function consist of 7 functions.
a. function header as intorduction of the system
b. function footer to display a copyright.
c. function getInteger.
(this function will ask user to enter an integer then return the value)
d. function add .
( this function will have 2 passing parameter , do addition operation and return the result)
e. function sub .
( this function will have 2 passing parameter , do subtract operation and return the result)
f. function mul .
( this function will have 2 passing parameter , do multiply operation and return the result)
g. function dev .
( this function will have 2 passing parameter , do devide operation and return the result)
From main function your have to call the function accodingly.
Exercise 5
Write a c program to calculate cgpa for a student
( refer to the Exercise 3)
Using function ask user to enter no of subject.
Exercise 6
Write a C program of Mark Conversion System. The program will convert from mark to grade point or from mark to grade for any mark entered by user.
The conversion table is given below:
GRADE GPA MARKS
A 4.00 80-100
B 3.67 75-79
C 3.33 70-74
D 3.00 65-69
E 2.67 60-64
F 2.33 55-59
G 2.00 50-54
H 1.67 45-49
I 1.00 40-44
z 0.00 0-39
Note:
You are required to use AT LEAST 4 FUNCTIONS statements when writing the program.
Write a menu driven program which has the following options:
a. Mark to Grade Point (GPA) Conversion.
b. Mark to Grade (A, B ,C to Z ) Conversion.
o. Exit
The interface of the system should be as follows:
Mark Conversion System for British Malaysian Institute Sdn. Bhd.
-------------------------------------------------------------------------------
a. Mark to Grade Point (GPA) Conversion
b. Mark to Grade (A, B, C to Z ) Conversion
o. Exit
------------------------------------------------------------------------------
Please enter your choice :
If .a. is entered:
-------------------------------------------------------------------------------
Mark Conversion System for British Malaysian Institute Sdn. Bhd
Mark to Grade point (GPA)
--------------------------------------------------------------------------------
Please enter the mark: 80.0
For the mark= 80.0 the GPA is 4.00
---------------------------------------------------------------------------------
then the program shall return to the menu
If .b. is entered:
-------------------------------------------------------------------------------
Mark Conversion System for British Malaysian Institute Sdn. Bhd
Mark to Grade ( A, B, C to Z)
--------------------------------------------------------------------------------
Please enter the mark: 80.0
For the mark= 80.0 the Grade is A
---------------------------------------------------------------------------------
then the program shall return to the menu
The program MUST check whether the value of unit input by the user is positive, zero or negative.
Error message MUST be displayed if zero or negative values are typed in.
If .o. is entered, the program shall end.
Otherwise, the program shall return to the first screen by mentioning wrong choice is entered.
Name your file as group_mark_conversion_your_name_function.c (e.g. L01_mark_conversion_yosofkadase_function.c)
and Submit your work by upload your file here: http://yosof.net
No comments:
Post a Comment