Written by students who passed Immediately available after payment Read online or as PDF Wrong document? Swap it for free 4.6 TrustPilot
logo-home
Class notes

FUNCTION IN C++

Rating
-
Sold
-
Pages
0
Uploaded on
24-04-2025
Written in
2024/2025

int main() { int sum = add(5, 3); cout "Sum: " sum; } ``` --- *Advantages of Using Functions:* - Code reusability - Easy debugging - Logical structure - Shorter main function --- Let me know if you'd like a handwritten-style format or notes for a specific topic like *recursion*, *function overloading*, or *inline functions*!

Show more Read less
Institution
Course

Content preview

FUNCTIONS IN C++
• A function is a group of statements that together perform a task.
Every C++ program has at least one function, which is main ().
• A function takes input, does some specific computation, and
produces output.
• The idea is to put some commonly or repeatedly done tasks
together to make a function so that instead of writing the same
code again and again for different inputs, we can call this function.
• In simple terms, a function is a block of code that runs only when
it is called.
• There are two types of function:


Standard Library Functions: Predefined functions in C++
User-defined Function: Created by users


Library Function
Library functions are also called “built-in Functions“. These functions
are part of a compiler package that is already defined and consists of a
special function with special and different meanings. Built-in Function
gives us an edge as we can directly use them without defining them
whereas in the user-defined function we have to declare and define a
function before using them.
For Example: sqrt(), setw(), strcat(), etc.
C++ User-defined Function
C++ allows the programmer to define their own function.
A user-defined function groups code to perform a specific task and that
group of code is given a name (identifier).
When the function is invoked from any part of the program, it all
executes the codes defined in the body of the function.

,Syntax of Function

return_type function_name (parameter_list)
{
//C++ Statements
}
A simple function example
#include <iostream>
int sum(int num1, int num2)
{
int num3 = num1+num2; return num3;
return num3;
}
int main()
{
cout<<sum(1,99);
return 0;
}

Output:
100

The same program can be written like this:

#include <iostream>
int sum(int,int); // function declaration
int main()
{
cout<<sum(1,99); //function call
return 0;
}

,int sum(int num1, int num2)
{
int num3 = num1+num2; // function definition
return num3;
}

it we will see there are three parts for functions such as function
declaration, function definition and calling of function.
Function Declaration: when we define a function before the main()
function in our program then we don’t need to do function
declaration but if we are writing our function after the main()
function then we need to declare the function first, else we will get
compilation error. Function declaration is also known as function
prototype.

syntax of function declaration:

return_type function_name(parameter_list);

Note: While providing parameter_list we can avoid the parameter
names. That is, int sum(int,int); instead of int sum(int num1,int
num2);.

Function definition: Writing the full body of function is known as
defining a function.

syntax of function definition:

return_type function_name(parameter_list)
{
//Statements inside function
}

, Calling function: We can call the function like this:

function_name(parameters);

Parameters and Arguments

Information can be passed to functions as a parameter.
Parameters act as variables inside the function.
Parameters are specified after the function name, inside the
parentheses.
we can add as many parameters as you want, just separate them
with a comma:
Syntax
void functionName(parameter1, parameter2, parameter3)
{
// code to be executed
}

The following example has a function that takes
a string called fname as parameter. When the function is called, we
pass along a first name, which is used inside the function to print the
full name:
Example
void myFunction(string fname)
{
cout << fname << " Refsnes\n";
}

int main() {
myFunction("Liam");
myFunction("Jenny");
myFunction("Anja");
return 0;

Written for

Institution
Course

Document information

Uploaded on
April 24, 2025
Number of pages
Unknown
Written in
2024/2025
Type
Class notes
Professor(s)
Unknown
Contains
All classes

Subjects

$13.69
Get access to the full document:

Wrong document? Swap it for free Within 14 days of purchase and before downloading, you can choose a different document. You can simply spend the amount again.
Written by students who passed
Immediately available after payment
Read online or as PDF

Get to know the seller
Seller avatar
sαηтнσмтs1

Get to know the seller

Seller avatar
sαηтнσмтs1 JCT COLLEGE
Follow You need to be logged in order to follow users or courses
Sold
-
Member since
1 year
Number of followers
0
Documents
1
Last sold
-

0.0

0 reviews

5
0
4
0
3
0
2
0
1
0

Recently viewed by you

Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Working on your references?

Create accurate citations in APA, MLA and Harvard with our free citation generator.

Working on your references?

Frequently asked questions