Lecture Notes On Functions C Programming
Lecture Notes On Functions C Programming
Benefits of Functions[2]
At times, a certain portion of codes has to be used many times. Instead of re-writing the
codes many times, it is better to put them into a "subroutine", and "call" this "subroutine"
many time - for ease of maintenance and understanding. Subroutine is called method (in
Java) or function (in C/C++).
The benefits of using functions are:
Divide and conquer: construct the program from simple, small pieces or
components. Modularize the program into self-contained tasks.
Avoid repeating codes: It is easy to copy and paste, but hard to maintain and
synchronize all the copies.
Software Reuse: you can reuse the functions in other programs, by packaging
them into library codes.
Two parties are involved in using a function: a caller who calls the function, and the
function called. The caller passes argument(s) to the function. The function receives
these argument(s), performs the programmed operations within the function's body, and
returns a piece of result back to the caller. (see Figure 1)
Syntax of a Function
returnedValuetype functionName( parameterList ) // function header
{
local variable declarations; // if any
statement1;
statement2;
.
.
.
statementN;
body of a function
Function Definition[3]
The syntax for function definition is as follows:
returnValueType functionName ( parameterList )
{
functionBody ;
}
The parameterList consists of comma-separated parameter-type and parametername, i.e., param-1-type param-1-name, param-2-type param-2-name,...
The returnValueType specifies the type of the return value, such as int or double. An
special return type called void can be used to denote that the function returns no
value. In C++, a function is allowed to return one value or no value (void).
Having the prototype available before the first use of the function allows the compiler to
check that the correct number and type of arguments are used in the function call and
that the returned value, if any, is being used reasonably.
Syntax of a Function Declaration
returnedValuetype functionName ( parameterList );
Sample Problem 1:
Write a program that would ask a user two numbers and how to process the numbers,
either add or subtract. The program should display the answer of the users desired
choice.
Flowchart (Design):
Implementation (Code):
/* Write a program that would ask a user two numbers and how to process the numbers,
either add or subtract. The program should display the answer of the users desired
choice. */
#include<iostream> // pre-processor directives
#include<conio.h>
#include<stdlib.h>
using namespace std;
main()
{
int num1, num2, choice, sum, diff; // local variable declaration
system ("cls");
cout << "Enter two numbers: ";
cin >> num1 >> num2;
cout << "What do you want to do with the numbers?\n";
cout << "[1] Add \n[2] Subtract";
cout << "\nEnter your choice: ";
cin >> choice;
switch(choice)
{
case 1: sum=num1+num2;
cout << "\nThe sum is " << sum;
break;
case 2: diff=num1-num2;
cout << "\nThe difference is " << diff;
break;
default: cout << "Invalid input.";
}
getch();
}
Sample Problem 2:
Write a program that would ask a user two numbers and how to process the numbers,
either add or subtract. The program should display the answer of the users desired
choice. In addition, the program would ask the user if he/she would like to try again. The
program would continuously run the whole process until he/she decides to exit the
program.
10
11
/* The program calls the main( ) function as a way of looping process to repeat the
program until the user chooses to end the execution. */
#include<iostream> // pre-processor directives
#include<conio.h>
#include<stdlib.h>
using namespace std;
int Add(); // function declaration or function prototype
int Subtract();
int num1, num2, choice, sum, diff; //global variable declaration
char tryagain;
main()
{
system ("cls");
cout << "Enter two numbers: ";
cin >> num1 >> num2;
cout << "What do you want to do with the numbers?\n";
cout << "[1] Add \n[2] Subtract";
cout << "\nEnter your choice: ";
cin >> choice;
switch(choice)
{
case 1: Add(); break;
case 2: Subtract(); break;
default: cout << "Invalid input.";
}
cout << "\n\nWould you like to try again?[Y/N] ";
cin >> tryagain;
if ((tryagain=='Y')||(tryagain=='y'))
main(); // the main( ) function is called to repeat the process.
else
{
system("cls");
cout << "Thank you for using the Kemberler software. ^_^";
}
getch();
}
int Add() // function definition
{
sum=num1+num2;
cout << "\nThe sum is " << sum;
}
12
int Subtract()
{
diff=num1-num2;
cout << "\nThe difference is " << diff;
}
References:
[1]
[2][3][5][6][7]
http://www.ntu.edu.sg/home/ehchua/programming/cpp/cp1_Basics.html
[4]
http://opencbp.sourceforge.net/en_US.ISO8859-1/books/opencbook/func.prototypes.html
13