0
0
C++programming~5 mins

Function calling in C++

Choose your learning style9 modes available
Introduction

Function calling lets you run a set of instructions by name. It helps you reuse code and keep programs organized.

When you want to repeat a task multiple times without rewriting code.
When you want to break a big problem into smaller, manageable parts.
When you want to test or fix one part of your program separately.
When you want to share code between different parts of your program.
Syntax
C++
functionName(arguments);
Replace functionName with the name of the function you want to run.
Put any needed information inside the parentheses as arguments.
Examples
Calls a function named printHello that takes no arguments.
C++
printHello();
Calls a function named sum with two numbers, 5 and 10.
C++
sum(5, 10);
Calls multiply with 3 and 4, and stores the returned value in result.
C++
int result = multiply(3, 4);
Sample Program

This program defines a function greet that prints a message. In main, it calls greet to show the message.

C++
#include <iostream>
using namespace std;

void greet() {
    cout << "Hello, friend!" << endl;
}

int main() {
    greet();  // Call the greet function
    return 0;
}
OutputSuccess
Important Notes

Functions must be declared or defined before you call them.

If a function returns a value, you can use it or store it when calling.

Arguments must match the function's expected type and order.

Summary

Function calling runs code inside a function by its name.

It helps reuse and organize your program.

Call functions with the right arguments to get results or actions.