Académique Documents
Professionnel Documents
Culture Documents
Functions
1
Today’s lecture outline
• Introduction to structure programming
• Function definition
• Function call
• Function prototype
2
Structured programming
• It enables programmers to break complex systems into
manageable components
• In C++, these components are known as functions
• A function is a block of statements that perform a task
• Every C++ program can be thought of as a collection of these
functions
3
Structured programming concepts
• Top-down design
• Code reusability
• Information hiding
4
Why Use Functions ?
Why write separate functions at all? Why not squeeze the entire logic into one function, main( )?
Two reasons:
(a) Writing functions avoids rewriting the same code over and over. Suppose you have a section
of code in your program that calculates area of a triangle. If later in the program you want to
calculate the area of a different triangle, you won’t like it if you are required to write the same
instructions all over again. Instead, you would prefer to jump to a ‘section of code’ that calculates
area and then jump back to the place from where you left off. This section of code is nothing but a
function.
(b) Using functions it becomes easier to write programs and keep track of what they are doing.
If the operation of a program can be divided into separate activities, and each activity placed in a
different function, then each could be written and checked more or less independently. Separating
the code into modular functions also makes the program easier to design and understand.
5
Function
• A function is a self-contained block of statements that perform a task
• Every C++ program can be thought of as a collection of these functions
6
Function call and definition
main()
Function
{
call message(); Waiting
cout<<“Hello world \n”;
}
message()
Function {
definition cout<<“Message function \n”;
}
7
Example program 1
Cout<<“I am in Main Function \n”;
Go to program
9
Example program 2
Cout<<“I am in Main\n”;
Cout<<“I am in Italy\n”;
10
Function prototype
• Function prototypes tell C how your function will be built and
used
• Function prototype contains following things about the
function:
– The data type returned by the function
– The number of parameters received
– The data types of the parameters
– The order of the parameters
11
Cont.
• It is not always necessary
– to send input as parameters to functions
– to have functions return values
• In such case programmer mention that function are void of
parameter and return value
12
Complete program
Function
prototype
Function
call
Function
definition
13
Important points
• C program is a collection of one or more functions
• A function gets called when the function name is followed by a
semicolon. For example,
14
Cont.
• The order in which the functions are defined in a program and the order
in which they get called need not necessarily be same. For example,
15
`
16
Function prototype (declaration)
18
Function Definition
• The function itself is referred as function definition. Function
definition in the above program is:
• // Function definition
int add(int a,int b)
{
int add;
add = a + b;
return add;
}
19
Cont
• When the function is called, control is transferred to the first
statement of the function body.
• Then, other statements in function body are executed
sequentially.
• When all codes inside function definition is executed, control
of program moves to the calling program.
20
Passing Arguments to Function
21
Notes on passing arguments
• The numbers of actual arguments and formals argument should be
the same.
• The type of first actual argument should match the type of first
formal argument. Similarly, type of second actual argument should
match the type of second formal argument and so on.
• You may call function a without passing any argument. The number(s)
of argument passed to a function depends on how programmer want
to solve the problem.
• In the above program, both arguments are of int type. But it's not
necessary to have both arguments of same type.
22
Find the maximum number using functions
23
Example 1
• Write a program with a function that takes two int parameters,
adds them together, then returns the sum. The program should
ask the user for two numbers, then call the function with the
numbers as arguments, and tell the user the sum.
• https://en.wikibooks.org/wiki/C%2B
%2B_Programming/Exercises/Functions#EXERCISE_1
24
1. Find out square of a number using a function?
2. Write a function to calculate the factorial value of any integer entered through the keyboard.
3. Write a function power ( a, b ), to calculate the value of a raised to b.
4. Write a function which receives a float and an int from main( ), finds the product of these two
and returns the product which is printed through main( ).
5. Write a function that receives 5 integers and returns the sum, average of these numbers. Call
this function from main( ) and print the results in main( ).
6. Write a function that receives marks received by a student in 3 subjects and returns the
average and percentage of these marks. Call this function from main( ) and print the results in
main( ).
7. Simple program to find the smallest number in C++.
8. Find factorial using functions?
25