Geza Kovacs
#include <iostream> utilizing namespace sexually transmitted disease;
int fundamental() { int threeExpFour = 1; for (int i = 0; i < 4; i = i + 1) { threeExpFour = threeExpFour * 3; } cout << "3^4 is " << threeExpFour << endl; return 0; }
#include <iostream> utilizing namespace sexually transmitted disease;
int fundamental() { int threeExpFour = 1; for (int i = 0; i < 4; i = i + 1) { threeExpFour = threeExpFour * 3; } cout << "3^4 is " << threeExpFour << endl; int sixExpFive = 1; for (int i = 0; i < 5; i = i + 1) { sixExpFive = sixExpFive * 6; } cout << "6^5 is " << sixExpFive << endl; return 0; }
Duplicate glue coding
#include <iostream> utilizing namespace sexually transmitted disease;
int primary() { int threeExpFour = 1; for (int i = 0; i < 4; i = i + 1) { threeExpFour = threeExpFour * 3; } cout << "3^4 is " << threeExpFour << endl; int sixExpFive = 1; for (int i = 0; i < 5; i = i + 1) { sixExpFive = sixExpFive * 6; } cout << "6^5 is " << sixExpFive << endl; int twelveExpTen = 1; for (int i = 0; i < 10; i = i + 1) { twelveExpTen = twelveExpTen * 12; } cout << "12^10 is " << twelveExpTen << endl; return 0; }
Duplicate glue coding (terrible)
#include <iostream> utilizing namespace sexually transmitted disease;
/some code which raises a discretionary whole number/to a subjective force
int principle() { int threeExpFour = raiseToPower(3, 4); cout << "3^4 is " << threeExpFour << endl; return 0; }
With a capacity
With a capacity
#include <iostream> utilizing namespace sexually transmitted disease;
/some code which raises a self-assertive whole number/to a subjective force
int fundamental() { int threeExpFour = raiseToPower(3, 4); cout << "3^4 is " << threeExpFour << endl; int sixExpFive = raiseToPower(6, 5); cout << "6^5 is " << sixExpFive << endl; return 0; }
With a capacity
#include <iostream> utilizing namespace sexually transmitted disease;
/some code which raises a subjective whole number/to a self-assertive force
int primary() { int threeExpFour = raiseToPower(3, 4); cout << "3^4 is " << threeExpFour << endl; int sixExpFive = raiseToPower(6, 5); cout << "6^5 is " << sixExpFive << endl; int twelveExpTen = raiseToPower(12, 10); cout << "12^10 is " << twelveExpTen << endl; return 0; }
Why characterize your own particular capacities?
• Readability: sqrt(5) is clearer than duplicate gluing in a calculation to register the square root • Maintainability: To change the calculation, simply change the capacity (versus transforming it all over you ever utilized it) • Code reuse: Lets other individuals use calculations you've executed
Capacity Declaration Syntax
int raiseToPower(int base, int type) { int result = 1; for (int i = 0; i < type; i = i + 1) { result = result * base; } return result; }
Capacity name
Capacity Declaration Syntax
int raiseToPower(int base, int type) { int result = 1; for (int i = 0; i < type; i = i + 1) { result = result * base; } return result; }
Return sort
Capacity Declaration Syntax
int raiseToPower(int base, int example) { int result = 1; for (int i = 0; i < type; i = i + 1) { result = result * base; } return result; }
Contention 1
• Argument request matters: – raiseToPower(2,3) is 2^3=8 – raiseToPower(3,2) is 3^2=9
Capacity Declaration Syntax
int raiseToPower(int base, int type) { int result = 1; for (int i = 0; i < type; i = i + 1) { result = result * base; } return result; }
Contention 2
• Argument request matters: – raiseToPower(2,3) is 2^3=8 – raiseToPower(3,2) is 3^2=9
Capacity Declaration Syntax
int raiseToPower(int base, int type) { int result = 1; for (int i = 0; i < type; i = i + 1) { result = result * base; } return result; }
signature
Capacity Declaration Syntax
int raiseToPower(int base, int type) { int result = 1; for (int i = 0; i < example; i = i + 1) { result = result * base; } return result; }
body
Capacity Declaration Syntax
int raiseToPower(int base, int type) { int result = 1; for (int i = 0; i < example; i = i + 1) { result = result * base; } return result; } Return articulation
#include <iostream> utilizing namespace sexually transmitted disease;
int raiseToPower(int base, int example) { int result = 1; for (int i = 0; i < type; i = i + 1) { result = result * base; } return result; }
int principle() { int threeExpFour = raiseToPower(3, 4); cout << "3^4 is " << threeExpFour << endl; return 0; }
Capacity conjuring
Capacity assertion
Giving back a worth
• Up to one worth may be returned; it must be the same sort as the arrival sort.
int foo() { return "hi";/blunder }
char* foo() { return "hi";/alright }
Giving back a worth
• Up to one worth may be returned; it must be the same sort as the arrival sort. • If no qualities are returned, give the capacity a void return sort
void printNumber(int num) { cout << "number is " << num << endl; }
int primary() { printNumber(4);/number is 4 return 0; }
Giving back a worth
• Up to one worth may be returned; it must be the same sort as the arrival sort. • If no qualities are returned, give the capacity a void return sort – Note that you can't proclaim a variable of sort void
int primary() { void x;/ERROR return 0; }
Giving back a worth • Return explanations don't as a matter of course should be toward the end. • Function returns when an arrival explanation is executed.
void printNumberIfEven(int num) { if (num % 2 == 1) { cout << "odd number" << endl; return; } cout << "considerably number; number is " << num << endl; }
int principle() { int x = 4; printNumberIfEven(x);/much number; number is 3 int y = 5; printNumberIfEven(y);/odd number }
Contention Type Matters
• printOnNewLine(3) works • printOnNewLine("hello") won't aggregate
void printOnNewLine(int x) { cout << x << endl; }
Contention Type Matters
• printOnNewLine(3) won't assemble • printOnNewLine("hello") works
void printOnNewLine(char *x) { cout << x << endl; }
Contention Type Matters
• printOnNewLine(3) works • printOnNewLine("hello") additionally works
void printOnNewLine(int x) { cout << x << endl; }
void printOnNewLine(char *x) { cout << x << endl; }
Capacity Overloading
• Many capacities with the same name, however distinctive contentions • The capacity called is the one whose contentions coordinate the conjuring
void printOnNewLine(int x) { cout << "Whole number: " << x << endl; }
void printOnNewLine(char *x) { cout << "String: " << x << endl; }
Capacity Overloading
• printOnNewLine(3) prints "Number: 3" • printOnNewLine("hello") prints "String: hi"
void printOnNewLine(int x) { cout << "Number: " << x << endl; }
void printOnNewLine(char *x) { cout << "String: " << x << endl; }
Capacity Overloading
• printOnNewLine(3) prints "1 Integer: 3" • printOnNewLine(2, 3) prints "2 Integers: 2 and 3"
void printOnNewLine(int x) { cout << "1 Integer: " << x << endl; }
void printOnNewLine(int x, int y) { cout << "2 Integers: " << x << " and " << y << endl; }
• Function presentations need to happen before summons
int foo() { return bar()*2;/ERROR - bar hasn't been proclaimed yet }
int bar() { return 3; }
• Function presentations need to happen before summons – Solution 1: reorder capacity assertions
int bar() { return 3; }
int foo() { return bar()*2;/alright }
• Function announcements need to happen before summons – Solution 1: reorder capacity statements – Solution 2: utilize a capacity model; educates the compiler you'll actualize it later
int bar();
int foo() { return bar()*2;/alright }
int bar() { return 3; }
capacity model
• Function models ought to coordinate the mark of the strategy, however contention names don't make a difference
int square(int);
int cube(int x) { return x*square(x); }
int square(int x) { return x*x; }
capacity model
• Function models ought to coordinate the mark of the strategy, however contention names don't make a difference
int square(int x);
int cube(int x) { return x*square(x); }
int square(int x) { return x*x; }
capacity model
• Function models ought to coordinate the mark of the system, however contention names don't make a difference
int square(int z);
int cube(int x) { return x*square(x); }
int square(int x) { return x*x; }
capacity model
• Function models are for the most part put into discrete header documents – Separates determination of the capacity from its execution
/myLib.h - header/contains models
int square(int); int 3D shape (int);
/myLib.cpp - execution #include "myLib.h"
int cube(int x) { return x*square(x); }
int square(int x) { return x*x; }
Recursion
• Functions can call themselves. • fib(n) = fib(n-1) + fib(n-2) can be effortlessly communicated by means of a recursive execution
int fibonacci(int n) { if (n == 0 || n == 1) { return 1; } else { return fibonacci(n-2) + fibonacci(n-1); }
Recursion
• Functions can call themselves. • fib(n) = fib(n-1) + fib(n-2) can be effectively communicated by means of a recursive execution
base case
int fibonacci(int n) { if (n == 0 || n == 1) { return 1; } else { return fibonacci(n-2) + fibonacci(n-1); }
Recursion
• Functions can call themselves. • fib(n) = fib(n-1) + fib(n-2) can be effectively communicated by means of a recursive execution
recursive step
int fibonacci(int n) { if (n == 0 || n == 1) { return 1; } else { return fibonacci(n-2) + fibonacci(n-1); }
Worldwide Variables
• what number times is capacity foo() called? Utilize a worldwide variable to decide this. – Can be gotten to from any capacity
int numCalls = 0;
void foo() { ++numCalls; }
int primary() { foo(); foo(); foo(); cout << numCalls << endl;/3 }
Worldwide variab