100% satisfaction guarantee Immediately available after payment Both online and in PDF No strings attached
logo-home
Summary C++ FUNCTIONS $5.49   Add to cart

Summary

Summary C++ FUNCTIONS

 6 views  0 purchase

The document details the use of functions in C++, a cornerstone of structured programming. It covers function declaration, definition, and invocation, explaining return types and parameter passing (by value and by reference). Practical examples illustrate common uses, such as modularizing code and ...

[Show more]

Preview 3 out of 18  pages

  • July 25, 2024
  • 18
  • 2023/2024
  • Summary
All documents for this subject (10)
avatar-seller
peterkuria3
ELITE TUTORING




C++ Functions
A function is a block of code that performs a specific task.

Suppose we need to create a program to create a circle and color it. We can
create two functions to solve this problem:

• a function to draw the circle

• a function to color the circle

Dividing a complex problem into smaller chunks makes our program easy to
understand and reusable.

There are two types of function:

1. Standard Library Functions: Predefined in C++
2. User-defined Function: Created by users




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.

,ELITE TUTORING



C++ Function Declaration

The syntax to declare a function is:

returnType functionName (parameter1, parameter2,...) {
// function body
}


Here's an example of a function declaration.


// function declaration
void greet() {
cout << "Hello World";
}


Here,

• the name of the function is greet()

• the return type of the function is void

• the empty parentheses mean it doesn't have any parameters

• the function body is written inside {}




Calling a Function

In the above program, we have declared a function named greet() . To use
the greet() function, we need to call it.
Here's how we can call the above greet() function.

int main() {

// calling a function
greet();

, ELITE TUTORING




}




How Function works in C++




Example 1: Display a Text
#include <iostream>
using namespace std;

// declaring a function
void greet() {
cout << "Hello there!";
}

int main() {

// calling the function
greet();

return 0;
}

Output

The benefits of buying summaries with Stuvia:

Guaranteed quality through customer reviews

Guaranteed quality through customer reviews

Stuvia customers have reviewed more than 700,000 summaries. This how you know that you are buying the best documents.

Quick and easy check-out

Quick and easy check-out

You can quickly pay through credit card or Stuvia-credit for the summaries. There is no membership needed.

Focus on what matters

Focus on what matters

Your fellow students write the study notes themselves, which is why the documents are always reliable and up-to-date. This ensures you quickly get to the core!

Frequently asked questions

What do I get when I buy this document?

You get a PDF, available immediately after your purchase. The purchased document is accessible anytime, anywhere and indefinitely through your profile.

Satisfaction guarantee: how does it work?

Our satisfaction guarantee ensures that you always find a study document that suits you well. You fill out a form, and our customer service team takes care of the rest.

Who am I buying these notes from?

Stuvia is a marketplace, so you are not buying this document from us, but from seller peterkuria3. Stuvia facilitates payment to the seller.

Will I be stuck with a subscription?

No, you only buy these notes for $5.49. You're not tied to anything after your purchase.

Can Stuvia be trusted?

4.6 stars on Google & Trustpilot (+1000 reviews)

82956 documents were sold in the last 30 days

Founded in 2010, the go-to place to buy study notes for 14 years now

Start selling
$5.49
  • (0)
  Add to cart