Download as pdf or txt
Download as pdf or txt
You are on page 1of 30

Programming Fundamentals

Control Structure
Functions

SHAHBAZ NAZEER
Functions - Outline

 Types of Functions
1. Built in Functions
2. User Defined Function
 User Defined Function
1. Function Declaration or Prototype
i. Return Type
ii. Function Name
iii. Parameters

2. Function Definition
i. Before main() function
ii. After main() function
iii. In a separate file

3. Function Call
4. Scope of a Function
Books

 Deitel & Deitel :– C++ How to Program


 Robert Lafore : Object-Oriented Programming in C++
 IT Series : Object Oriented Programming Using C++

 in case of any Query Email at shahbaznazeer@gcuf.edu.pk


 Please Avoid Sending messages on mobile.
Control Structures

 Control Structures are just a way to specify flow of control in programs.


 Any algorithm or program can be more clear and understood if they
use self-contained modules called as logic or control structures.
 It basically analyzes and chooses in which direction a program flows
based on certain parameters or conditions.
 There are three basic types of logic, or flow of control, known as:
1. Sequence logic, or sequential flow
2. Selection logic, or conditional flow
3. Iteration logic, or repetitive flow

4. Function Call
Functions

 When we begin programming in C/C++


 We generally write one main() function and write all our logic inside this function.

 This approach is fine for very small programs, but as the program size grows, this
become unmanageable.

 So we use functions. We write code in the form of functions.

 The main() function always acts as a driver function and calls other functions.

 When the program starts it always enters in main function first.


Functions

 Functions are used to accomplish some task many times without writing the same code
again and again.

 A function is a named block of code or group of statements that performs a specific task.

 Statements written inside a function are executed each time the function is called by its name.

 Each function must have a unique name.

 Function are the building blocks of programming.

 A function is known with various names like a method or a sub-routine or a procedure etc.
Importance of Functions

 A Program may need to repeat the same block of code more than one times

 OR it may require certain task to be performed repeatedly.

 Such code which needs to be executed repeatedly is stored separately in a function.

 Functions can divide the whole program into multiple parts. These parts of a program can be
managed more easily.

 Advantages of Functions
-- Easier to Code -- Easier to Maintain
-- Easier to Modify -- Reusability
-- Less Time Consuming
Types of Functions

C++ Language Provides two types of functions.

1. Built in Functions
2. User Defined Functions.
Types of Functions

 C++ Language Provides two types of functions.


1. Built in Functions
 Programmers can use library functions by invoking the functions directly.
 Programmers don't need to write code of the functions themselves.
2. User Defined Functions.
 C++ allows the programmer to define their own function.
 A user-defined function to is used to perform a specific task and that block of code is given a
name (identifier).
 When the function is invoked from any part of the program, it executes the code defined in the
body of the function.
Types of Functions

Built in Functions
Built in Functions

 Programmers can use library functions by invoking the functions directly.


 They don't need to write the functions themselves.
 In order to use library functions,
 we usually need to include the header file in which library functions are defined.
 For Example, in order to use mathematical functions such as sqrt() and abs(), we
need to include the header file cmath.
 Built in functions are also known as
 Standard Library Functions or
 Predefined functions.
Built in Functions - Example

 Example: C++ Program to find


the Square Root of a number.
 In this program, the sqrt() library
function is used to calculate the
square root of a number.

 The function sqrt() is defined in the


cmath header file. That's why we
need to use the code
#include <cmath> to use the sqrt()
function.
Types of Functions

User-Defined Functions
User-Defined Functions

 A type of function written by the programmer is known as user defined


function.

 C++ allows the programmer to define their own function.


o A user-defined function groups code to perform a specific task.
o Block of code is given a unique name (identifier).
o When the function is invoked from any part of the program,
o it executes all the statements defined in the body of the function.
User-Defined Functions – TYPES

A user defined function consists of the following.

1. Function Declaration or Prototype


2. Function Definition
User-Defined Functions

Function Declaration
1 - Function Declaration or Prototype

 Function declaration is a model of a function.


 It provides information to compiler about structure of the function.
 It Tells the compiler about function name and how to call the function.
 Function declaration consists of the following.
1. Function Return Type
2. Function Name
3. Number and Types of Parameters
1 - Function Declaration Example

1. The name of the function is greet


2. The return type of the function is void ( means nothing to return ).
3. The empty parentheses ( ) mean it doesn't have any parameters.
4. The function body is written inside { } curly braces.

 Note: We will learn about returnType and parameters later.


Function Call
Function Call

 The statement that activates a function is known as a function call. A function is called
with its name followed by parenthesis ( ).
 A function can be called from anywhere in the program. i.e from main() or any other
function.
 The function that calls another function is know as calling function.
 When a function is called, Following steps take place.
1. Control moves to the function that is called.
2. All statements in the function executed.
3. Control returns back to the point
where the function was called (calling function).
Function Call – Example Explained

1. We have declared a function named greet.


2. To use the greet function, we need to call it.
3. Inside int main() function we call the
greet function. With the statement greet();
i. Here main() is the calling function.
ii. and greet() is the called function.
4. Control jumps to the greet() function and
executes the statements inside it. After
execution of greet() function control returns
back to main() .
User-Defined Functions

Function Definition
2 - Function Definition

 A set of statements that explains what a function does is called function definition.
 A Function definition contains everything that a function declaration contains and additionally it
also contains the body of the function

Function Declaration void greet( )


Function Definition {
// statement(s)
Function Body cout<<“Welcome to GCUF”;
}

int main()
{
Function Call greet(); // function call
return 1;
}
2 - Function Definition - Types

 A function definition can be written at the following places in a program.


1. Before main( ) Function

 No changes needed. We write complete function before main( ).

2. After main( ) function

 Function prototype is required before main( )

3. In a separate file

 Need to #include the relevant file before main( ).


Function Definition (2) – After main( )

 function definition After main( ) function


1. When a program executes control always enters in the main( ) function first.

2. So, if a function is written after main( ). Then the function cannot be executed as
main( ) doesn’t have any information about how to call the function.

 Function declaration provides required information like return Type, Name and Parameters.

3. If we want to write function definition after main( ) , then we need to copy function
declaration (without body ) before main( ) function.

 In this scenario its terminated with a semi-colon ; Which also known as function prototype.
Function Definition – Before & After main( )

Before main function After main function


void greet( ) void greet( ) ; // function Prototype
{
// statement(s) int main()
cout<<“Welcome to GCUF”; {
} greet(); // function call
return 1;
int main() }
{
greet(); // function call void greet( )
return 1; {
} // statement(s)
cout<<“Welcome to GCUF”;
}
Function Definition (2) - After main( )

Function Prototype void greet( ) ; // function Prototype

main Function int main()


{
Function Call greet(); // function call
return 1;
}

Function Declaration void greet( )


Function Definition {
// statement(s)
Function Body cout<<“Welcome to GCUF”;
}
Function Definition (3) – In a separate file

1. Functions can be defined in a separate file.

2. You are required to #include the relevant file before main( ).

greet.h file Main program output

Welcome to GCUF
Happy New Year 2021
Home Work

 Write short note on scope of a function.


1. Local Functions
2. Global Functions
Implement given programs using functions

1. Write a C++ program to check whether a given number is even or odd.

2. Write a C++ program to check whether a given number is positive or negative.

3. Write a C++ program to find whether a given year is a leap year or not.

4. Write a C++ program to read the age of a candidate and determine whether it is eligible for casting
his/her own vote. (18+ age is required to cast vote).

5. Write a C++ program which takes 3 numbers from user and find the largest of three numbers.

6. Write a program in C to read any day number in integer and display day name in the word.

7. Write a program in C to read any Month Number in integer and display the number of days for this
month.

You might also like