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.
Consider the figure above. When a program begins running, the system calls the
How user-defined function works 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 functionadd()
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
}
OutputEnters two integers: 8 -4 Sum = 4
1 comments:
Click here for commentsUser-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
Show Konversi KodeHide Konversi Kode Show EmoticonHide Emoticon