A function is a collection of statements that accept input, perform some specific computation, and return output. The idea is to combine some frequently or repeatedly performed tasks into a function so that instead of writing the same code for different inputs, we can call the function.
return-type function-name(parameter1, parameter2, ...)
{
// function-body
}
A function can be declared by enclosing its return type, name, and arguments inside brackets. It notifies the compiler that this function is present. If you want to define a function after the main function in C++, you must declare it first.
A function definition defines the function’s body. A function’s declaration and definition can be done concurrently, but it should be done before calling it.
#include <iostream>
using namespace std;
//declaring the function
int multi(int x, int y);
int main()
{
int a = 10;
int b = 20;
int c = multi (a, b); //calling the function
cout << c;
}
//defining the function
int multi (int x, int y)
{
return (x * y);
}
200
When you define a function, you tell it what to do and how to use it; you must call or invoke the function. When a function is called from the main function, control is transferred to the function that was called. The function then goes about its business. When the task is completed, the control is returned to the main function. Methods for calling functions:
You only pass the copies of the variable to the function in this calling method, not the actual argument to the function. Because the variable or arguments are copied, any changes made to the variable in the function have no effect on the actual argument.
#include <iostream>
using namespace std;
//declaring the function
int multi(int x);
int main()
{
int a = 10;
multi (a); //calling the function
cout <<"a: "<< a;
}
//defining the function
int multi (int x)
{
return (x * x);
}
a: 10
In this calling technique, you pass the argument’s address or reference, and the function receives the argument’s memory address. In this case, the variable’s actual value changes, or you can say that it reflects the changes back to the variable.
#include <iostream>
using namespace std;
//declaring the function
void multi(int *x);
int main()
{
int a = 10;
multi (&a); //calling the function
cout <<"a: "<< a;
}
//defining the function
void multi (int * x)
{
*x= ((*x) * (*x));
}
a: 100
Note: also read about Storage classes in C++
If you like my post please follow me to read my latest post on programming and technology.
https://www.instagram.com/coderz.py/
https://www.facebook.com/coderz.py
Staying up to the mark is what defines me. Hi all! I’m Rabecca Fatima a keen learner, great enthusiast, ready to take new challenges as stepping stones towards flying colors.
Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…
Given an integer A. Compute and return the square root of A. If A is…
Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…
A heap is a specialized tree-based data structure that satisfies the heap property. It is…
What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…