User-defined Function

C++ allows 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 that function is invoked from any part of program, it all executes the codes defined in the body of function.

How user-defined function works in C Programming?

Working of function in C++ programming
Consider the figure above. When a program begins running, the system calls the main() function, that is, the system starts executing codes from main() function. When control of program reaches to function_name() inside main(), the control of program moves to void function_name(), all codes inside void function_name() is executed and control of program moves to code right after function_name() inside main() as shown in figure above.

Example 1: Function

C++ program to add two integers. Make a function add() to add integers and display sum in main() function.

# include <iostream>
using namespace std;

int add(int, int);            //Function prototype(declaration)

int main() {
    int num1, num2, sum;
    cout<<"Enters two numbers to add: ";
    cin>>num1>>num2;
    sum = add(num1,num2);         //Function call
    cout<<"Sum = "<<sum;
    return 0;
}

int add(int a,int b) {            //Function declarator
    int add;
    add = a+b;
    return add;                 //Return statement
}
Output
Enters two integers: 8
-4
Sum = 4


Previous
Next Post »

1 comments:

Click here for comments
Anonymous
April 12, 2022 at 4:10 PM ×

User-Defined Function ~ System Help Line >>>>> Download Now

>>>>> Download Full

User-Defined Function ~ System Help Line >>>>> Download LINK

>>>>> Download Now

User-Defined Function ~ System Help Line >>>>> Download Full

>>>>> Download LINK 4L

Selamat Anonymous dapat PERTAMAX...! Silahkan antri di pom terdekat heheheh...
Balas
avatar
admin
Thanks for your comment