0% found this document useful (0 votes)
35 views2 pages

Final Homework #1

A function is a block of code that performs a specific task. There are two types of functions: predefined/library functions provided by the language itself (like printf()), and user-defined functions created by the programmer. Functions contain four parts - a return type, name, parameter list (optional), and body. Functions reduce complexity in programs by breaking code into reusable units. Examples show a user-defined function to add numbers and a predefined sqrt() function to calculate pizza diameter from area.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views2 pages

Final Homework #1

A function is a block of code that performs a specific task. There are two types of functions: predefined/library functions provided by the language itself (like printf()), and user-defined functions created by the programmer. Functions contain four parts - a return type, name, parameter list (optional), and body. Functions reduce complexity in programs by breaking code into reusable units. Examples show a user-defined function to add numbers and a predefined sqrt() function to calculate pizza diameter from area.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
You are on page 1/ 2

1.

A function is a code that is used to perform a single, related action like printf() and main() that
we usually see in a code. These are called built-in functions provided by the language itself. A
function conains a unit of code that works on various inputs and produces concrete results.
2. The types of function are predefined function or library function and user defined function.
Pre defined function are functions that created by its own library, for example is the printf() that
is used as a standard library function to display output on the screen. The user defined function
is created according to the situation by the programmer, it reduces the complexity of a big
program and complexity of code.
3. The parts of the function are:
(a) Return type – a data type of the value that the function returns.
(b) Function name – the actual name of the function. The function name and the parameter
list together constitute the function signature.
(c) Parameter list – this is like a placeholder referring to the type, order, and number of the
parameter of a functions. This is optional meaning that a function may contain no parameters.
(d) Function body – this contains a collection of statements that defines what the function does.

4. The example of program using function are:


(a) user-defined function

#include <stdio.h>
int addNumbers(int a, int b); // function prototype

int main()
{
int n1,n2,sum;

printf("Enters two numbers: ");


scanf("%d %d",&n1,&n2);

sum = addNumbers(n1, n2); // function call


printf("sum = %d",sum);

return 0;
}

int addNumbers(int a, int b) // function definition


{
int result;
result = a+b;
return result; // return statement
}

(b) pre defined function


#include <iostream>
#include <cmath>
using namespace std;

int main ( )
{
const double PI = 3.141592653589793;
double area, diameter;
cout << "Enter the area of the pizza in square inches : ";
cin >> area;
diameter = 2.0 * sqrt(area/PI);
cout << "The pizza has a diameter of :" << diameter << endl;

return 0;
} //end main

You might also like