[PDF] [PDF] C++ Programming

D Schubert C++ Programming “A function declaration introduces the function name and its type Note unsigned is a shorthand for unsigned int ▫ Task



Previous PDF Next PDF





[PDF] Signed and Unsigned Types in Interfaces

plicit size-changing member functions), it would also be convenient to provide a function that returns the current size of an Array object If you're like most C++ 



C++ Basics

All C/C++ programs begin execution with a function known as main() A-11) are the essence of most functional programming unsigned u; // modifies int Fig



[PDF] Lecture 021: Basic C++ Syntax

Mainly used as a return type for functions that do not return any value • Pointers to void Identified by C++ keywords signed char and unsigned char • Minimum  



[PDF] Dont add to the signed/unsigned mess - Open-std

14 fév 2019 · The root problem is that in C and C++ signed and unsigned integers don't where elem_count() is a function that takes a container or a range 



[PDF] C++ Programming

D Schubert C++ Programming “A function declaration introduces the function name and its type Note unsigned is a shorthand for unsigned int ▫ Task



[PDF] Secure Coding in C and C++

unsigned integers ○ bit pattern is preserved—no lost data ○ high-order bit loses its function as a sign bit ○ If the value of the signed integer is not negative,  



[PDF] C++ Intro

function – Statements execute one after the next and end with a semicolon (;) – Ends with a 'return Integer Types (signed by default unsigned with optional



[PDF] HOT 06

void sw_sort_data(unsigned char *data, unsigned int n); } We also require a main() function, from which we will call the C++ functions, and an array of width char 



[PDF] C/C++ Language Reference - Altium

26 jui 2000 · standard C and C++ library functions Several of these types can be modified using signed, unsigned, short, long, unsigned long long int 

[PDF] unsigned in c++

[PDF] unsigned int 1

[PDF] unsigned int exploit

[PDF] unsigned int signed long

[PDF] unsigned integer

[PDF] unsigned integer in cpp

[PDF] unsigned keyword in cpp

[PDF] unsigned long in cpp

[PDF] unsigned short c++

[PDF] unsupervised clustering sklearn

[PDF] unsupervised learning

[PDF] unsupervised learning pdf

[PDF] unsw how to write an annotated bibliography

[PDF] unts montevideo convention

[PDF] unvalidated data in an http response header

Lecture 2

Software Engineering Group

Philipp D. Schubert

C++ Programming

1.Functions

2.std::string

3.std::vector

4.Containers

5.Pointer and reference types

Contents

The notion of a function

"function declaration introduces the function name and its type. A function definition associates the function

name/type with the function body[en.cppreference.com] "Example: maximum function "Declaration intmax(int,int); intmax(inta,intb);// or with formal parameter names "Definition intmax(inta,intb) { if(a >=b)returna; "Some languages allow definition only (e.g. Java) "We will learn about the use of declarations in the next lecture

What is a function?

"A function is a little machine "Gets input "Manipulates input "Returns output "Think of it as a functional unit! "Very similar to a mathematical function "Task "Declare a function ݂that is able to sum two numbers ݔǡݕא "Define this function ݂to actually sum two numbers ݔǡݕא

Functions in maths& C++

"Declaration in C++ "unsignedf(unsigned,unsigned); "Definition in C++ "unsignedf(unsignedx,unsignedy){returnx +y;} "Note unsignedis a shorthand for unsigned int "Task "Declare a function ݂that is able to sum two numbers ݔǡݕא "Define this function ݂to actually sum two numbers ݔǡݕא "Declaration in mathematics "Definition in mathematics "Pretty much the same!

Functions in C++

"A function declaration in C++ " (); " contains zero or more formal parameters "A function declaration is sometimes called a signature or function head "A function definition in C++ " () { "A function definition has to have a body "Function body has to be enclosed by braces

Functions in C++

"Note "A function may return nothing! "A function may receive no parameters! voidf();CC YRLG LV M ³VSHŃLMO´ type voidg(inta); voidh(void); intreturnAlwaysOne(){return1;} "General rule: name things after their purposes, same holds for variables! "Function´s in- "Build-in types "User-defined types (today and next time)

Functions in C++

"What is the value of result after the function call? "intresult =function(2,4); "16 "What does the function do? "Implements the power function "What would be a better declaration? "intpow(intbase,intexponent); "Note this function "intresult =pow(2.5,4.8); "Significant figures get cut off "Lets define a function "Why you should use meaningful names: intfunction(intx,inty){ intresult =x; for(inti =2;i <=y;++i){ result *=x; returnresult;

Use of functions

intpow(intbase,intexponent){ intresult =base; for(inti =2;i <=exponent;++i){ result *=base; returnresult; "Use a functions to "perform a logically task "that has to be performed multiple times "build an abstraction / generalization "structure your source code "The task described by a function can be reused! "Programming becomes faster "Less opportunities for errors "More readable "Use libraries: a collection of useful functions

Use of functions

"What is that? intfactorial(intn){ if(n >1)returnn *fac(n-1); return1; "Compute the factorial function using recursion! "Let´s consider the factorial function! "Sequential intfactorial(intn) { intf =n; while(n--> 1) f *=n; returnf;

Conditional assignments

"If an assignment depends on a conditions you can use a short hand inti=...// some value intvariable; if(i>10) variable =100; else variable =0; // shorthand which does the same intvariable =(i>10)?100 :0; "Note there are many of these shorthands "c++; "d +=10; "unsigned// which is shorthand for unsigned int "You will get used to it

Recursion

"With the notion of a function one can use recursion!

"Recursionoccurs when a thing is defined in terms of itself or of its type. Recursion is used in a variety of

disciplines ranging from linguisticsto logic. The most common application of recursion is in mathematicsand

computer science, where a functionbeing defined is applied within its own [en.wikipedia.com] "Now for real "A recursive function uses itself to solve a task! "A function exhibits recursive behavior if

1.it defines one (or more) base case(s) that do not use recursion

2.a set of rules that reduce all other cases toward the base case

[Image from http://giphy.com/]

Factorial function revisited

factorial(2) if (2 > 1) return 2 * factorial(1); factorial(1) if (1 > 1) NO! return 1;

We have reached the base case!

The call to factorial(5) can now evaluate

5*4*3*2*1 = 120

"If you are still not convinced have a look at: "What on Earth is Recursion? Computerphile "Recursion often allows elegant solutions "Requires some time to get used to intfactorial(intn){ if(n >1)returnn *factorial(n-1); return1; "What happens if factorial() gets called? intresult =factorial(5); "Let´s see what happens: factorial(5) if (5 > 1) return 5 * factorial(4); factorial(4) if (4 > 1) return 4 * factorial(3); factorial(3) if (3 > 1) return 3 * factorial(2);

Functions

intfactorial(intn){ return(n >1)?n*fac(n-1):1; voidprintInt(inti){ cout< usingnamespacestd; intmain(){ inti=factorial(5); intj =factorial (6); printInt(i); printInt(j); return0;

A note on functions

"With constexprwe effectively have to versions: "a constexprversion "a non-constexpr-version // can be evaluated at compile time constexprinti=factorial(8); intx =...// non-constant x // can only be evaluated at run time intj =factorial(x); "Actual parameters passed to a function are copied by default! "Inside of a function you work with copies by default! intincrement(intx){returnx++;} intx =10; inty =increment(x);// y is now 11 // x is still 10 "Remember constexpr // C++11 allows one return statements constexprintaddNumbers(inta,intb){ returna +b;} // C++14 allows more than one statement constexprintfactorial(intn){ intresult =1; while(n--> 0) result *=n; returnresult;

A note on functions

"Function calls have some costs in terms of performance "Safe register contents, put function arguments on stack, increment stackpointer perform jump back "If performance really matters compiler can inline small functions "A function call is replaced with copying the functions body to the call-site "Use keyword inline to give the compiler some hints inlineintadd(inta,intb){returna +b;} // such a call to add() intc =add(10,20); // is replaced with intc =10 +20; "Inliningonly necessary in rare cases (sometimes you make it worse) "Compiler inlineson its own if compiler optimizations are turned on (-Ox flag, where x is 1,2 or 3)

Local and global variables

"Global variables are accessible across functions (and modules) "A variable is global if it is not defined inside a function "Example inti=10; doubled =1.234; voidprintGobals(){ cout<A note on global variables "Try to avoid global variables as much as possible "You rarely need them "They break local reasoning "It is harder to understand the code "It is hard to parallelize code that makes heavy use of globals

User-defined types/ non-build-in data types

"Learn about two very important user-defined types "std::string "std::vector "Both contained in the standard template library (STL) "Vector is perhaps the most used non-build-in data type "Define your own datatypes "Use classor struct "Next lecture! std::string "But remember build-in arrays are dangerous "What if you lost the size of that array?quotesdbs_dbs14.pdfusesText_20