Professional Documents
Culture Documents
Programming Lab - II
EXPERIMENT NO-1
Title: Write a program to implement basic arithmetic operations using functions.
Objectives:
Theory:
What is Function:
• A function is a block of code that performs some operation.
• A function can optionally define input parameters that enable callers to pass
arguments into the function.
• A function can optionally return a value as output.
• Functions are useful for encapsulating common operations in a single reusable
block.
• Ideally with a name that clearly describes what the function does.
If you’re using a library function, the function has already been defined and compiled for you.
Also, you can use a standard library header file to provide the prototype. All that’s left to do is
call the function properly. For example, the standard C library includes the strlen() function for
finding the length of the string. The associated standard header file cstring contains the function
prototype for strlen() and several other string-related functions.
But when you create your own functions, you have to handle all three aspects—defining,
prototyping, and calling—yourself
Example: 1
Here’s the output of the program main() will call the simple() function:
Output: I’m but a simple function.
You can group functions into two categories:
• Function that don’t have return values. Functions without return values
are termed type void functions
• Function that return values.
Example 2: In below example cheers and cube two functions are given.
• The cheers function takes int as internal parameter but doesn’t return any value.
• The cube function takes double as internal parameter and return cube of given
number in the form of double. So, double is return type of cube function.
Procedure:
1. Write a program to accept two integers.
2. Declare function prototype for arithmetic operations like: add, sub, mul, div;
3. Write function definition for all function prototypes
4. Call these methods from main method and perform operation like: addition, subtraction,
multiplication, division.
Key Concepts: function prototype, arguments, function call, function definition.