>a>>b; cout<<"\n the maximum one is: "< Download presentation Presentation is loading. Please wait. Published byLorenzo Mirandela Figueiroa
Modified over 6 years ago
1
Function User defined function is a code segment (block) that perform an specific action and may return a value. Function Definition: Return_DT F_name ( list of formal parameters) { Action_ body ; } If Return_DT is void Then: Action_body doesn’t contain return statement. Else: Action_body contains return statement. Calling (invoking) Function: F_name (Actual parameters);
2
Example #include <iostream.h> int Max (int Value1, int Value2) {
3
Example #include <iostream.h> int Max (int Value1, int Value2) {
4
Example #include <iostream.h> int Sum (int A, int B) {
5
Example #include <iostream.h> bool Positive (int Num) {
6
Example #include <iostream.h> float Area (int R) {
7
Example #include <iostream.h> long Power(int Base, int Exp) {
8
Example #include <iostream.h> long Fact (int Num) {
9
Void Returned Data Type
10
Example #include <iostream.h> int Mul(int V1, int V2) {
11
Function Prototype #include <iostream.h> int Mul(int, int);
12
Call by value When calling, the value of actual parameter will be copied to the formal parameter. #include <iostream.h> void Increment(int); void main() { int A = 10; Increment(A); cout<<A<<endl; } void Increment(int X) { ++X; }
13
Call By reference When calling, the reference formal parameter will be an alternative name to the actual parameter. #include <iostream.h> void Increment(int&); void main() { int A = 10; Increment(A); cout<<A<<endl; } void Increment(int &X) { ++X; }
14
Call By reference When calling, the pointer formal parameter will points to the actual parameter. #include <iostream.h> void Increment(int*); void main() { int A = 10; Increment(&A); cout<<A<<endl; } void Increment(int *X) { ++*X; }
15
Recursion Function call itself #include <iostream.h>
16
Example #include <iostream.h> int Zap (int N) { int Z;
17
Array as parameter -Array name is pointer (call by reference)
18
Array as parameter -Array element (call by value)
Similar presentations © 2024 SlidePlayer.com. Inc. Log in
Function User defined function is a code segment (block) that perform an specific action and may return a value. Function Definition: Return_DT F_name.
Similar presentations
Presentation on theme: "Function User defined function is a code segment (block) that perform an specific action and may return a value. Function Definition: Return_DT F_name."— Presentation transcript:
if (Value1 > Value2) return Value1; else return Value2; } void main() { int a, b; cout<<"\nPlease Enter the values of a and b: "; cin>>a>>b; cout<<"\n the maximum one is: "<<Max(a,b)<<endl; Function Definition Calling the function in an expression like cout<<, condition, assignment statements
if (Value1 > Value2) return Value1; else return Value2; } void main() { int a, b; cout<<"\nPlease Enter the values of a and b: "; cin>>a>>b; cout<<"\n the maximum one is: "<<Max(a,b)<<endl;
return (A+B); } void main() { int N1, N2, S; cout<<"\n Please Enter N1 and N2: "; cin>>N1>>N2; S = Sum(N1,N2); cout<<"\nSum= "<<S<<endl;
if (Num > 0) return true; else return false; } void main() { int Number; cout<<"\nEnter Number: "; cin>> Number; if (Positive(Number)) cout<<"\n the number is positive"; cout<<"\n the number is negative"; cout<<endl;
return (3.14 * R * R ); } void main() { int Radius; cout<<"Enter the Redius: "; cin>>Radius; cout<<"\nCircle Area is: "<<Area(Radius); cout<<endl;
int M=1; for(int i=1; i<=Exp; i++) M*=Base; return M; } void main() { int B, E; cout<<"\nEnter Base: "; cin>>B; cout<<"\nEnter Exponent: "; cin>>E; cout<<"\n Result= "<<Power(B,E); cout<<endl;
int F = 1, i = Num; while (i>=1){ F *= i; i--; } return F; } void main() { int Number; cout<<"Enter an integer number: "; cin>>Number; cout<<endl<<Number<<"!= "<<Fact(Number); cout<<endl;
#include <iostream.h> void Print(char Ch, int n) { for (int i=1; i<=n; i++) cout<<Ch; cout<<endl; } void main() { char Sym; int Number; cout<<"\nEnter the Symbol: "; cin>>Sym; cout<<"\nHow many times: "; cin>>Number; Print(Sym,Number); No Return Statement
return V1 * V2; } void Result() { cout<<"\n5 x 9 = "<<Mul(5,9); cout<<"\n4 x 7 = "<<Mul(4,7); cout<<"\n6 x 4 = "<<Mul(6,4)<<endl; } void main() { Result() ; }
int Add(int, int); void Show(); void main() { Show(); } int Mul(int X, int Y) { return X*Y; } int Add(int X, int Y) { return X+Y; } void Show() { int A=10, B=20; cout<<Add(A,B)<<'\t'<<Mul(A,B)<<endl; } Function Prototype contains only data types But may contain identifiers.
int Fact (int N) { if (N<=1) return 1; else return N * Fact(N-1); } void main() { cout<<Fact(5)<<endl;
if (N<=1) Z=1; else Z= Zap(N-1) + Zap(N-3); return Z; } void main() { cout<<Zap(5)<<endl;
#include <iostream.h> void Increment (int a[]) { for (int i=0; i<5; i++) a[i] += 10; } void main() { int b[5] = {10,20,30,40,50}; Increment(b); for(int i=0; i<5; i++) cout<<b[i]<<'\t'; cout<<endl; }
#include <iostream.h> void Increment (int a) { ++a; } void main() { int b[5] = {10,20,30,40,50}; Increment(b[1]); cout<<b[1]<<endl; }
Similar presentations
All rights reserved.