C's USER DEFINED FUNCTION
Please keep this BLOG RUNNING. Wait for the ads to load, then Click Skip add at upper right.
Title:
C's USER DEFINED FUNCTION
Download the .Pdf Format of this article. Click HERE
PART
A - INTRODUCTION TO FUNCTION
v What is a
Function?
·
The concept of
functions in C language is the same as for any other programming language.
·
Few languages
have methods but, since there are no classes in C, methods are not used.
·
Functions are
best used for reusable code, or code that you know works and do want make use
of it throughout development.
·
If you find
yourself tempted to use copy and paste on a particular block of code, even
once, then you should move the code into a function.
·
A Function can
either return a value after performing a task or just perform
a task without returning a value.
v Declaring and
Defining Functions
·
Functions are declared
similar to regular variables.
SYNTAX (Function
declaration without a return value):
<data type>
function_name(Parameters); /*Function header*/
SYNTAX (Function
declaration with a return value):
<data type> function_name(Parameters); /*Function
header*/
·
As you can see,
the only difference is the parenthesis after the name of the function. However,
a declaration is not enough. A function
must also have DEFINITION. To completely declare a function you have to
have the following
SYNTAX:
<data type>
function_name(Parameters); /*Function Declaration */
<data type>
function_name(Parameters) /*Function Definition */
{
//Some code here/Body
of the Function
}
v Writing a
Function
The
first step in writing a function is to know what the function will do.
·
The Function Header
The Function Header has three components:
1) The Function Return Type <data type>
- The function return type specifies the data type that
the function returns to the calling program.
- The return type can be any of C's data types: char, int,
long, float, or double.
- A function that doesn't return a value must use a return
type of void. Here are some examples:
int func1(...) /* Returns a type int. */
float func2(...)
/* Returns a type float. */
void func3(...)
/* Returns nothing. */
2) The Function Name
- A function name must be unique (not assigned to any
other function or variable).
- It's a good idea to assign a name that reflects what the
function does.
3) The Parameter List
- A function may accept 1 or more parameters.
- Function parameters must follow rules in declaring
variables
·
The Function Body
-
The function body is enclosed in braces, and it
immediately follows the function header.
- It's here that the real work is
done. When a function is called, execution begins at the start of the function body and
terminates (returns to the calling program) when a return statement is encountered or when
execution reaches the closing brace.
-
The function body usually has two components:
1) Function Statements
-
There is essentially no limitation on the statements that can be included
within a function.
-
The only thing you can't do inside a function is define another function.
-
You can, however, use all other C statements, including loops, if statements,
and assignment statements.
-
You can call library functions and other user-defined functions.
2) Returning a Value
-
To return a value from a function, you use the return keyword, followed by a C expression.
-
When execution reaches a return statement, the expression is evaluated, and execution
passes the value back to the calling program.
-
The return value of the function is the value of the expression. Consider this
function:
int func1(int var)
{
int x;
/* Function code goes here... */
return x;
}
When
this function is called, the statements in the function body execute up to the return
statement. The return terminates the function and returns the value of x to the
calling
program. The expression that follows the return keyword can be any valid C expression.
A function can contain multiple return statements. The first return executed is the only
one that has any effect.
v Calling a Function
- There are two ways to call a function.
1) Any function can be called by simply
using its name and argument list alone in a statement, as in
the following example. If the function has a return value, it is discarded.
Sample with argument:
wait(12);
Sample
without argument:
wait();
2) The second method can be used only with functions that have a
return value. Because these functions evaluate to a
value (that is, their return value), they are valid C expressions and can be
used anywhere a C expression can be used. You've already seen an expression
with a return value used as the right side of an assignment
statement.
Here are some more examples:
In the following
example, half_of() is a parameter of a function:
printf("Half
of %d is %d.", x, half_of(x));
First, the function
half_of() is called with the value of x, and then printf() is called using the values
x and half_of(x).
In this second example,
multiple functions are being used in an expression:
y =
half_of(x) + half_of(z);
Although half_of() is
used twice, the second call could have been any other function. The following
code shows the same statement, but not all on one line:
a =
half_of(x);
b = half_of(z);
y = a + b;
v Scope and Lifetime of Variables in
Functions
A variable's scope can be either global or
local.
·
Global Variable
-
It means that a variable may be referenced anyplace in the entire program.
-
A global variable will also be available as long as the program is executing.
·
Local Variable means it can only be
referenced in the current block of code being executed.
v Recursion
·
The term recursion refers to a
situation in which a function calls itself either directly or indirectly.
·
Indirect recursion occurs when one
function calls another function that then calls the first function.
·
C allows recursive functions, and they
can be useful in some situations.
PART
B - FUNCTION-ORIENTED PROGRAM
v C Program
that uses a Function without a return value
·
A Program the
prints the word "Hello World!" when the function is called.
#include<stdio.h>
void hello(); /*Function
Declaration/Prototype */
main()
{
hello(); /*Calling the Function*/
}
void hello() /*Function Definition */
{
puts("Hello World!"); /*Function Statement*/
}
v C Program
that uses a Function with a return value
·
A Program the
computes the area of rectangle using a function.
#include<stdio.h>
int area(int len, int wid); /*Function
Declaration/Prototype */
main()
{
int l,w;
puts("Enter Lenght:
");
scanf("%i",&l);
puts("Enter Width:
");
scanf("%i",&w);
printf("The area of
the rectangle is %i", area(l,w));
/*Calling the Function*/
}
int area(int len, int wid) /*Function Definition
*/
{
int a;
a=len*wid; /*Function Statements*/
return a;
}
Workshop
This
Workshop provides quiz questions to help you solidify your understanding of the
material covered, and exercises to provide you with experience in using what
you've learned.
1) How
do I know what a good function name is?
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
2) When
variables are declared at the top of the listing, before main(), they can be
used anywhere, but local variables can be used only in the specific function.
Why not just declare everything before main()?
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
3) Does
main() have to be the first function in a program?
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
4)
How many values can a function return?
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
5)
If a function doesn't return a value, what type should it be
declared?
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
______________________________________________________________________
PART C- PROGRAM DEVELOPMENT
GRADING SYSTEM (10 points)
·
This program uses two functions. One is a function that returns a value
and a function that does not return a value.
·
This program also demonstrates the use of function that calls another
function.
Ø The program will ask for the
student's name, prelim, midterm and the tentative final grade. Entered grades
must be validated between 65 to 100 and prompt the user if out of range.
Ø The Final grade is computed by getting the 30% both for prelim and
midterm grades and 40% for the tentative final grade. This will use a function
named "fgrade" which accepts
three arguments and will return the final grade.
Ø The program must transmute
the corresponding grades and will print according to the following:
91 and
above = "A"
85 to 89 = "B"
80 to 84 = "C"
75 to 79 = "D"
74 and
below = "Failed"
This will use a
function named "transmute"
which will accept one parameter and will print
accordingly.
Ø After the computation is
done, the program should ask if the user would like to compute for another
grade, if Yes then will repeat the program and if not will terminate the
program.
Enter Student's Name: Juan Dela Cruz
Enter Prelim Grade: 85
Enter Midterm Grade:
90
Enter Tentative Final
Grade: 83
The Final grade is
85.7
The Equivalent of 85.7
is B grade.
Would you like to
compute for another student's grade? Y/N
mean
Please keep this BLOG RUNNING. Wait for the ads to load, then Click Skip add at upper right.
Title: C's USER DEFINED FUNCTION
Download the .Pdf Format of this article. Click HERE
No comments:
Post a Comment