Professional Documents
Culture Documents
5.1 Functions - Function Declaration and Definition
5.1 Functions - Function Declaration and Definition
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
4. Function Call
Functions
This approach is fine for very small programs, but as the program size grows, this
become unmanageable.
The main() function always acts as a driver function and calls other 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.
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
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
1. Built in Functions
2. User Defined Functions.
Types of Functions
Built in Functions
Built in Functions
User-Defined Functions
User-Defined Functions
Function Declaration
1 - Function Declaration or Prototype
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
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
int main()
{
Function Call greet(); // function call
return 1;
}
2 - Function Definition - Types
3. In a separate file
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( )
Welcome to GCUF
Happy New Year 2021
Home Work
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.