Advertisement
Guest User

Untitled

a guest
Feb 18th, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.90 KB | None | 0 0
  1. /// 5. functions
  2.  
  3. #include<bits/stdc++.h>
  4. using namespace std;
  5.  
  6. // function declaration
  7. // (return value type) function_name(type1 name1, type2 name2 ...){}
  8.  
  9. int pow(int a,int b){ // calculate (a) to the power of (b)
  10.     // send two (int) into the function and return a (int)
  11.    
  12.     int ans=1; // identity
  13.    
  14.     for(int i=1;i<=b;i++) // repeat (b) times
  15.         ans*=a; // means "ans = ans*a"
  16.    
  17.     return ans; // (ans) is the answer of pow(a,b)
  18. }
  19.  
  20. signed main(){
  21.  
  22.     int a,b; // The variable a,b int main() is different from
  23.              // the a,b in function pow(), they are independent
  24.              // variable
  25.    
  26.     cin >> a >> b;
  27.     cout << pow(a,b) << endl; // print the value of pow(a,b)
  28.    
  29.     int c=pow(a,b);
  30.     // It can also be assigned to the other variable
  31.    
  32.     cout << c << endl;
  33.    
  34.     /// ** Don't forget to add ';' at the end of the statement **
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement