You are on page 1of 25

Programming Fundamental

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”;

Cout<<“I am in Italy \n”;

Go to program

Cout<<“I am in Brazil \n”;

Cout<<“I am in argentina \n”;


8
Points to remember
• C program must contains at least one function
• Execution of C program begins with main() function.
• If there are more than one function then one function must be
main()
• There is no limit on number of functions in C++ program
• Functions in a program are called in sequence as mentioned in
main() function
• After the execution of function control returns to main()

9
Example program 2
Cout<<“I am in Main\n”;

Cout<<“Finally I am back in main function \n”;

Cout<<“I am in Italy\n”;

Cout<<“I am in back in Italy \n”;


I am in main
I am in italy
Cout<<“I am in Brazil \n”;
I am in brazil
I am in argentina
I am back in italy
Cout<<“I am in argentina \n”;
I am finally back in main
Press any key to continue

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,

• A function is defined when function name is followed by a pair of braces


in which one or more statements may be present

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)

• If a user-defined function is defined after main() function,


compiler will show error. It is because compiler is unaware of
user-defined function, types of argument passed to function
and return type.
• In C++, function prototype is a declaration of function without
its body to give compiler information about user-defined
function. Function prototype in the above example is:

• int add(int, int);


17
Function Call
• To execute the codes of function body, the user-defined
function needs to be invoked(called).
• In the above program, add(num1,num2); inside main() function
calls the user-defined function.
• The function returns an integer which is stored in variable add.

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

You might also like