[go: up one dir, main page]

0% found this document useful (0 votes)
11 views22 pages

User-Defined Functions

Uploaded by

ihossain1212n
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views22 pages

User-Defined Functions

Uploaded by

ihossain1212n
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

EEE 123

USER-DEFINED FUNCTIONS

Prepared by Dr. Mehdi Hasan Chowdhury, EEE, CUET


Developing Large Programs
• Difficult to manage large code
• Teams of people may be involved
• Easy to forget what the project is all about
• People may leave the company

Result: Code should be


– well organized
– well commented and documented
– Organized in a modular form (small coherent parts)
Top-Down Design
• Begin with the main problem/idea
• Divide it into smaller problem/ideas
• Further divide each small problem/idea into smaller
more manageable parts.

The process is termed: Divide and Conquer


When to stop?
– When the sub-problem/idea is understood and manageable
– Coding rule of thumb – a module is 5-30 lines (one page)
Top-Down Module Example
Functions as Modules of Code
Functions are the basic modules in C

• Every program has a main function


• The main function uses other functions
• Each function may use other functions
• Functions help organize and develop code
Function and Organization
What is a Function ?
• A function is a module (piece of code)
aimed at accomplishing a single task
– Note: the scope of the task or its objective may
large (recall breakdown of a problem/ideas)
What is a Function ?
Functions : Calling and Data
• Calling function passes zero or more
parameters to called function

• Called function “returns” only ONE item


main passes parameters to function1
function1 passes parameters to functiona
Predefined/coded functions
Library
We have already used a few:
printf ( ) scanf( )

• Somebody has already written these.


– Note that we need not know how they work.

• We do need to know what the functions do and


how to use them
– documentation
Libraries of Code
• Often languages come with libraries of code

• Provided “free of charge”


– Beware of Copyright issues...

• Use library code whenever possible


– software is expensive (design, coding, testing, documentation,
etc).
Using a Standard C Library
User Defined Functions
Three places where a function name is used:

1. Declaration (Prototype)

2. Call (Invocation)

3. Definition (Specification)
Function Declarations, Calls & Definitions
#include <stdio.h>
void greeting (void); /* Function Prototype*/

int main(void)
{
greeting( ); /* Function Call*/
return 0;
}

/* Function Definition*/

void greeting(void)
{
printf ("\n Hello World \n");
}
Declarations, Calls & Definitions
Example of Fn. Declarations
Example: A function to calculate hypotenuse.
#include <stdio.h>
#include <math.h>
/* Function Prototype */
double hypot (double x , double y);
int main () {
double side1, side2;
printf(“Enter 2 sides of Right triangle\n”,);
scanf(“%lf%lf”, &side1, &side2);
printf(“The hypotenuse is: %f\n“,
hypot(side1,side2));
return 0;
}
Example of Function Definition
Example: The Actual function to hypotenuse.

/*Function Hypotenuse */
/*Input: Two sides of a Right triangle */
/*Output: The Hypotenuse */

double hypot(double x , double y)


{
double hypotenuse = sqrt( x*x + y*y);
return (hypotenuse);
}
Using the function’s declaration the compiler compares the
declaration to each occurrence of the function in the program.

a. Number of parameters must be the same


b. Type of parameters must be the same

hypotenuse = hypot(side1,side2);

double hypot(double x , double y)


The return statement

A return statement should be used even if there is


nothing to return.

void myfunction(…….)

/*ends with the statement */

return;
Implicit Declaration
• If a function definition is not available then
– C creates an implicit function definition
– function parameters and return value are int.

You might also like