0% found this document useful (0 votes)
9 views11 pages

Function

Function

Uploaded by

sudarshanpp22
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views11 pages

Function

Function

Uploaded by

sudarshanpp22
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Function

Prof. A.P.Khan
Department of Computer Engineering
D.N.Patel College of Engineering, Shahada
Function

A function is a block of code which only runs


when it is called.
A large program in C++ can be divided to many
subprogram.
C enables its programmers to break up a program
into segments commonly known as functions.
You can pass data, known as parameters, into a
function.
2
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 subprogram is called as a function.
Basically a job of function is to do something.
To perform any task, we can create function. A
function can be called many times. It provides
modularity and code reusability.
C/C++ program contain at least one function which
is main(). 3
Advantage of functions in C++
By using functions, we can avoid rewriting same logic/code again
and again in a program.
We can call C++ functions any number of times in a program and
from any place in a program.
We can track a large C program easily when it is divided into
multiple functions.
Code Reusability:- By creating functions in C++, you can call it
many times. So we don't need to write the same code again and
again.
Code optimization:-It makes the code optimized, we don't need to
write much code. use functions, you need to write the logic only
once and you can reuse it several times.
4
return_type function_name
( parameter list )
{ body of the function }

1. Library Functions: are the functions


which are declared in the C++ header files
such as ceil(x), cos(x), exp(x), etc.

2. User-defined functions: are the functions


which are created by the C++ programmer, so
that he/she can use it many times. It reduces
complexity of a big program and optimizes
the code. 5
To use function in the program we need to establish three
elements that are related to the program
Function Declaration:- C need to be declared before they are
need to used. For the function the declaration need to be
before the first call function.
Function Definition:- It is actual code for the function to
perform the specific task.
Function Call:- we use function call to access the function.
The program or function that calls the function is referred to
as calling program or calling function.
6
Simple function call
#include<isotream.h>
printf_msg(); // function declaration
void main() // Library function
{
print_msg(); //function call
}
print_msg() // function definition
{
cout<<“This is a module called printf message”;
}
7
#include<stdio.h>
void sum();
void main()
{
cout<<"\nGoing to calculate the sum of two numbers:";
sum();
}
void sum()
{
int a,b;
cout<<"\nEnter two numbers";
cin>>a>>b;
cout<<"The sum is :-”<<a+b;
}

8
Function without argument and with return value.
#include<iostream.h>
int sum();
void main()
{
int result;
cout"\nGoing to calculate the sum of two numbers:";

result = sum();
cout<<result;
}
int sum()
{
int a,b;
cout<<"\nEnter two numbers";
cin>>a>>b;
return a+b;
} 9
#include<stdio.h>
int even_odd(int); }
void main() int even_odd(int n)
{ {
int n,flag=0; if(n%2 == 0)
cout<<“\nGoing to check whether a number is even or odd"; {
cout<<"\nEnter the number: ”; return 1;
cin>>n; }
flag = even_odd(n); else
if(flag == 0) {
{ return 0;
cout<<“\nThe number is odd"; }
} }
else
{
cout<<“The number is even";
}
10
THANKS!
Any questions?
+91-9893092930
[email protected]

11

You might also like