Functions
CS 111 - Computer
Programming
Functions
• A function is a block of code that performs a specific task. It is used to
divide a problem into smaller parts to make program easy to understand.
Functions
General Form
type name ( parameter1, parameter2, ...) { statements }
type – is the type of data the function will return. (e.g., int, void, String)
name – is the identifier by which the function can be called
parameter1, ... – is the input(s) your function needs to do its job.
statements – is the steps (code) that the function will do when it's called.
Functions with
Parameters
Functions
type name ( parameter1, parameter2, ...) { statements }
int add(int a, int b) {
return a + b;
}
Now, instead of writing a + b all the time, we can just do:
int sum = add(5, 3); // sum becomes 8
// function example
#include <iostream>
using namespace std;
int addition (int a, int b)
{
int r;
r=a+b; int addition (int a, int b)
return r;
}
z = addition (5,3);
int main ()
{
int z;
z = addition (5,3);
cout << "The result is "
<< z;
}
// function example
#include <iostream>
using namespace std;
int subtraction (int a, int b)
{
int r;
r=a-b;
return r;
}
int main ()
{
int x=5, y=3, z;
z = subtraction (7,2);
cout << "The first result is " << z << '\n';
cout << "The second result is " << subtraction (7,2) << '\n';
cout << "The third result is " << subtraction (x,y) << '\n’;
cout << "The fourth result is " << z= 4 + subtraction (x,y); <<
'\n';
}
Practice Exercise
Let us return the product of two numbers using a function with return type
and parameters.
Algorithm:
1. Declare function syntax with the name multiply(int a, int b)
2. Return type of the function is int
3. The function has two parameters: int a and int b
4. Function body is defined inside { ... }
5. Inside the function, multiply the two parameters and return the result
6. Call the function inside int main() using multiply(...) with actual
parameters
Void
Function
Functions with NO Type
General Form
void functionName(parameter1, parameter2, ...) {
statements; }
// void function example
#include <iostream>
using namespace std;
void printmessage ()
{
cout << "I'm a function!";
}
int main ()
{
printmessage ();
}
#include <iostream>
using namespace std;
void PrintMessage();
int main(void)
{
char name[10];
cout << "What is your first name? ";
cin >> name;
cout << "\n\nHello " << name << endl << endl;
// Here is the function call
PrintMessage();
}
// Following is the function definition - function heading and code
void PrintMessage()
{
cout << "********************\n";
cout << " Welcome to my\n";
cout << " wonderful program!\n";
cout << "********************\n";
}
// display a number
void displayNum(int num1, double num2) {
cout << "Integer number: " << num1 << endl;
cout << "Decimal number: " << num2 << endl;
}
int main() {
int n1 = 10;
double n2 = 10.75;
// calling the function
displayNum(n1, n2);
return 0;
}
Practice Exercise
Let us display “Hello world” using a void function.
Algorithm:
1. Declare function syntax with the name hello( )
2. Return type function void
3. No parameter for function name hello()
4. Function body inside {...}
5. Call function inside int main() using declared function name hello (); with
semicolon