Motivation Function Homework 6. Functions Ján Dugáček October 25, 2018 Ján Dugáček 6. Functions Motivation Function Homework Table of Contents 1 Motivation 2 Function Exercise Operators Generic functions Lambdas Exercise 3 Homework Homework Advanced homework Ján Dugáček 6. Functions Motivation Function Homework Motivation Can you think of a comfortable way to calculate A · (B · A) · C · A, where A, B and C are matrices expressed as arrays? Ján Dugáček 6. Functions Motivation Function Homework Exercise Operators Generic functions Lambdas Exercise Function f l o a t square ( f l o a t x ) { f l o a t powered = x ∗ x ; r e t u r n powered ; } // . . . f l o a t d i s t a n c e = square ( x − a ) + square ( y − b ) ; There is no keyword that defines a function Function declaration starts with the type it returns, then there’s its name followed by types of arguments it takes and it ends by a block of code Arguments have to be named in order to be used in the function The value behind the return keyword is the value returned by the function Ján Dugáček 6. Functions Motivation Function Homework Exercise Operators Generic functions Lambdas Exercise Function #2 std : : s t r i n g even ( i n t x ) { x = x % 2; i f ( x == 1) r e t u r n "no" ; e l s e r e t u r n " yes " ; } There can be any number of return statements, the first one the execution reaches exits the function Variable x is a copy of the argument the function received, changing it has no outside effect Two functions can have the same name as long as they have different argument types Ján Dugáček 6. Functions Motivation Function Homework Exercise Operators Generic functions Lambdas Exercise Function #3 void swapVars ( f l o a t& x , f l o a t& y ) { f l o a t o r i g = x ; x = y ; y = o r i g ; } The ampersand after the variable type (&) makes it a reference Editing a variable through a reference changes the variable used to call the function The void keyword means that no variable is returned and return is unnecessary Ján Dugáček 6. Functions Motivation Function Homework Exercise Operators Generic functions Lambdas Exercise Function #4 f l o a t sum( const std : : vector & vec ) { f l o a t t o t a l = 0; f o r ( f l o a t v a l : vec ) t o t a l += v a l ; r e t u r n t o t a l ; } References are mainly used to prevent copying large objects that would take a lot of time (such as containers, strings, ...) const is a modifier that prevents a variable from being edited; it’s useful to mark you don’t want to edit it and you will not be able to edit it accidentally Ján Dugáček 6. Functions Motivation Function Homework Exercise Operators Generic functions Lambdas Exercise Exercise 1 Write a function that checks if x is divisible by y and use it in an interactible program 2 Write a function that returns the average of numbers in a vector 3 Write a function that appends one vector at the end of another 4 Write a function that computes matrix multiplication 5 Calculate A · (B · A) · C · A, where A, B and C are matrices expressed as easy::vector T sum( const std : : vector & vec ) { T t o t a l = 0; f o r (T c u r r e n t : vec ) t o t a l += c u r r e n t ; r e t u r n t o t a l ; } Can be applied on any vector, but will not compile if the type in vector can’t be set to 0 or summed The type T is determined based on arguments when compiling If necessary, the type of T can be specified f l o a t sum = sum(theVector ) ; Ján Dugáček 6. Functions Motivation Function Homework Exercise Operators Generic functions Lambdas Exercise Lambdas f l o a t x = 2; auto func = [&] ( i n t y ) { x = ( x + y ) ∗ 2; }; func ( y ) ; Lambda functions are local functions that are stored in variables A lambda can have access to variables in its context Use [=] instead of [&] to use copies of the variables (it’s necessary if the lambda outlives these variables) Ján Dugáček 6. Functions Motivation Function Homework Exercise Operators Generic functions Lambdas Exercise Exercise 1 Write a function that sums numbers in two vectors of the same length, returning the vector of results (vector sum from algebra) 2 Change the function so that it could be called using the + operator Advanced exercices: 1 Create a cube function that calculates third power of any type of numeric variable (without using pow) 2 Write a vector-summing function that can be used on vectors of any numeric type (but both have the same type) 3 Change the function so that it could be called using the + operator Ján Dugáček 6. Functions Motivation Function Homework Homework Advanced homework Homework Write a vector subtracting function that removes all occurrences of elements in the vector in second argument (in sequence) from the vector in the first argument You have two weeks to do it Ján Dugáček 6. Functions Motivation Function Homework Homework Advanced homework Advanced Homework Create an operator * that can be used to multiply matrices of type std::array,S2> You have two weeks to do it template T sum( const std : : array a r r a y ) { T t o t a l = 0; f o r ( i n t i = 0; i < S ; i++) t o t a l += a r r a y [ i ] ; r e t u r n t o t a l ; } Ján Dugáček 6. Functions