Functions in C++ Part I

Channel:
Subscribers:
2,640
Published on ● Video Link: https://www.youtube.com/watch?v=w21Sgk3Fk-U



Duration: 1:09:23
173 views
2


Today we talked about a couple important things in C++, including the const keyword, why you shouldn't use floats in general circumstances, and the difference between the core C++ language and the standard library (code which comes with most every distribution of C++). We explored the standard library a bit and learned how to look up what header file things are in, and why you may or may not want to use bits/stdc++.h in your includes (compile time goes up).


But the big topic for today was the topic of functions. At the most basic level, they work like functions do in algebra. You pass in some parameters and get a result back out. (We'll talk next time about functions without parameters or return values.) For example, the abs() function takes in a number and returns the distance that number is from 0 (i.e. makes negative numbers positive).


You can make a function in C++ up above main by following this pattern:
return_type function_name(parameter list) { code; }


For example, if you wanted to write a function that squared a number you could do it something like this:
double square(double x) {
return x*x;
}







Tags:
csci 40
c++
const
functions
include