Professional Documents
Culture Documents
Unit 4: Functions
Notes
Structure
4.1 Introduction
4.2 The Main Function
4.3 General Form of a Function
4.4 Scope Rule of Functions
4.5 Simple Function
4.5.1 The Function Declaration
4.5.2 Calling the Function
4.5.3 The Function Definition
4.5.4 Eliminating the Declaration
4.6 Passing Arguments to Function
4.6.1 Passing Constants
4.6.2 Passing Variables
4.6.3 Passing by Value
4.6.4 Returning Values from Functions
4.6.5 Eliminating Unnecessary Variables
4.7 Reference Arguments
4.7.1 Passing Simple Data Types by Reference
4.8 Overload Functions
4.8.1 Different Number of Arguments
4.9 Inline Functions
4.9.1 Default Arguments
4.10 Return by Reference
4.11 Programming style
4.11.1Values vs. Identities
4.12 Access specifiers
4.13 Summary
4.14 Check Your Progress
4.15 Questions and Exercises
4.16 Key Terms
4.17 Further Readings
Objectives
After studying this unit, you should be able to:
• Understand the Simple function.
• Learn about arguments.
4.1 Introduction
A function groups a number of program statements into a unit and gives it a name. This
unit can then be invoked from other parts of the program.
Functions play an important role in program development. Dividing a program into
functions is one of the major principles of structured programming. Another use of
functions is that they reduce the size of a program by calling and using them at different
places in the program. Any sequence of instructions that appears in a program more
than once is a candidate for being made into a function. The function's code is stored
once in the memory, even though it is executed many times in the course of the
program.
Functions continue to be the building blocks of C++ programs. They serve the
same purpose as subprograms and procedures in BASIC. Infact, C++ has added many
new features to functions to make them more reliable and flexible.
The return-type specifies the type of data that the function returns. A function
may return any type of data except an array. The Parameter-list is a comma-separated
Notes list of variables names and their associated types that receive the values of the
arguments when the function is called. A function may be without parameter in which
case the parameter list is empty. However, even if there are no parameters, the
parentheses are still required. All function parameters must be declared individually,
each including both the type and name. The general form of a parameter declaration list
is:
f (type Var 1, type 2, ----------- type var n)
for example
f (int i, j, float k) [* incorrect*]
f (int i, int j, float k) [* correct*]
To make a function inline, the keyword inline is needed in the function definition:
inline float pdtokg (float pounds)
Notes
In INLINE, we must place the definition of function before main(). This is because
the compiler must insert the actual code into the program, not just instructions to call the
function.
Be aware that the inline keyword is actually just a request to the compiler.
Sometimes the compiler will ignore the request and compile the function as a normal
function.
Private:
The members declared as "private" can be accessed only within the same class and not
from outside the class.
Public:
The members declared as "public" are accessible within the class as well as from
outside the class.
Notes
Protected:
The members declared as "protected" cannot be accessed from outside the class, but
can be accessed from a derived class. This is used when inheritaance is applied to the
members of a class.
The members declared as "public" are accessible within the class as well as from
outside the class.
4.13 Summary
A function is a group of statements that together perform a task. Every C++ program
has at least one function, which is main(), and all the most trivial programs can define
additional functions. You can divide up your code into separate functions. How you
divide up your code among different functions is up to you, but logically the division
usually is so each function performs a specific task.
A function declaration tells the compiler about a function's name, return type, and
parameters. A function definition provides the actual body of the function. The C++
standard library provides numerous built-in functions that your program can call. For
example, function strcat() to concatenate two strings, function memcpy() to copy one
memory location to another location and many more functions. A function is knows as
with various names like a method or a sub-routine or a procedure etc
4. How many max number of arguments can present in function in c99 compiler?
a) 99
b) 90
c) 102
d) 127
#include <iostream>
void main()
void main()
cout<<"hai";
int main()
mani();
return 0;
}
a) Hai
b) haihai
c) compile time error
d) none of the mentioned
x = 20;
y = 10;
int main()
int x = 10;
fun(x, x);
cout << x;
return 0;
}
a). 10
b). 20
c). compile time error
d). none of the mentioned
8. What is the scope of the variable declared in the user definied function?
a) whole program
b) only inside the {} block
c) both a and b
d) none of the mentioned
• Main(): The main () function is the starting point for the execution of a
program
• Return type: The return-type specifies the type of data that the function
returns
• Local variable: Variables that are defined with in a function are called local
variables.