Learn to Code via Tutorials on Repl.it!

← Back to all posts
C wars: Chapter Six: Return of the function: A Finale
CodeLongAndPros

C wars: Chapter Six: The return of the function

Today, you'll learn about one of the most important programming concepts: Functions.

Functions are a reusable block of code.

You remember printf? He's a function.

You can define a function like this:

return_type name(args) {

So, for example: foo returns x plus 1:

"But", you ask, "Why do we need to return? What does it do for us?"

Well, you can use it like this:

y = foo(2)

y is now three.

META TIME!

Let's be a little meta with functions.

A function will evaluate to a return value.

Some common ones are:

  • int
  • double
  • char*
  • void

So x += foo(x);

Is really:

x += ++x;`

Back to the code

Ok. so now you know what a function is, let's talk about arguments.

An argument is a parameter given to a function.

Some examples of arguments:

  • foo(-> 10 <-)
  • printf(-> "%s\n" <-, -> "Hello" -<)
  • C vs C++

Now we need to talk about scope.

If you run this code:

What do you think will happen?

It will output 10.

This is because of scope.

Every var in a function cannot be accessed outside that function.

There are two ways to pass data between functions:

  • return
  • Pointers

We've talked about #1.

Now, on to #2:

Pointer arguments:

If we have this code:

It will output 7, since plus_seven is modifying the location where the value of x is.

Your homework:

  • Convert yesterday's program to a function, strlen, that does the same thing.
  • Make a program that uses a while loop or recursion to compute the factorial of x

And now for some news:

This is the last tutorial in C wars.

I'm thinking about writing a series on the Linux programming envroiment and the GNU toochain.

If you would be interested, please do this:

  • Comment it
  • Use VitrtualBox and make an Ubuntu VM. There are many good tutorials, but if you have any questions, just ask.
  • Install Vim, GCC, Make, and GDB (sudo apt update && sudo apt upgrade && sudo apt install vim build-essential)

(You don't need a Ubuntu VM, Arch users. Wait. Don't you have a Linux install?)

(WSL can work, but I do not support it's creation and want you to consider the consequences of using it.)

Well, so long, and thanks for all the cyclesfish!
Part One: The Segfault menace

Part Two: The data wars

Part Three: Revenge of the if

Part Four: A new loop

Part Five: The Empire points back

Part Six: Return of the function

Voters
renaudtp
elcorreodeangel
EpicGamer007
TheForArkLD
Highwayman
lightningrock
CodeLongAndPros
Comments
hotnewtop
EpicGamer007

I wish this series would continue :(.

One thing that I would love to see in the tutorial section is how to use the inbuilt libraries. Java, C#, C++, C, they all have a lot of inbuilt libraries like javax.swing or System.Generics, iostream, and 'stdlib.h'. I don't really see many tutorials explaining the most useful ones..

CodeLongAndPros

@AbhayBhat C actually has no standard library, it's provided by glibc on UNIX.

EpicGamer007
CodeLongAndPros

@AbhayBhat So, for example, DOS has conio.h and Linux does not.

CodeLongAndPros

@AbhayBhat I could do 'Programming in the UNIX environment (chapter 6 of K&R)' as one.

EpicGamer007

@CodeLongAndPros, go for it!

CodeLongAndPros

@AbhayBhat Gotta find K&R now, and finish school.

CodeLongAndPros

@AbhayBhat Hrm I feel like Bytewave now...

CodeLongAndPros

@AbhayBhat Actually, this one is pretty badly written, I can do better.

EpicGamer007

@CodeLongAndPros, ok but one suggestion, DONT make it a cycle farming one.

CodeLongAndPros

@AbhayBhat

Like this?

EpicGamer007

@CodeLongAndPros, no anon cycle farming one means dont make a series like "day 1", "day 2", "day 3". Put it all in one gian tutorial. It isn't exactly a good practice

CodeLongAndPros

@AbhayBhat Yeah, I had to do that with this because I just couldn't write it all in one.

Highwayman

It will output 7, since plus_seven is modifing the location where x is.

Em. Seems like a bit of a typo there...

Edit: or, well, more like it needs a bit more explaining more like it.

CodeLongAndPros

@Highwayman Updated

Highwayman

@CodeLongAndPros noice ty 👌

CodeLongAndPros

@Highwayman What do you think of a series on Linux programming and dev tools?

Highwayman

@CodeLongAndPros I know I for one would definitely appreciate that!

CodeLongAndPros

@Highwayman You know, except when I browse r/linuxmasterrace, I feel like I am the only Linux user on the internet.

Highwayman

@CodeLongAndPros hum this is kinda funny seeing as repl.it is just one massive Linux vm / Linux community in the end lol.

CodeLongAndPros

@Highwayman Well yeah and 60% of the web uses Linux, but 2020 is not the year of the Linux desktop

Highwayman

@CodeLongAndPros sadly, but hey, enough tutorials and one could probably make it the year of the Linux desktop 😜

CodeLongAndPros

@Highwayman Yeah, until M$ EEE's with WSL two.

Then we'll be waiting for the year of the FreeBSD desktop.

Highwayman
CodeLongAndPros

@Highwayman But seriously, WSL, and M$ Tax. All huge barriers to YOTLD

Highwayman

@CodeLongAndPros now you’be lost me. Care to explain?

Edit: you’ve not you’be

CodeLongAndPros

@Highwayman

So do you know what an EEE is?

Highwayman

@CodeLongAndPros emmm imma say no though maybe so no.

CodeLongAndPros
Highwayman

@CodeLongAndPros ahh ok I see.

Highwayman

@CodeLongAndPros honestly f windows I hate them so much sometimes

CodeLongAndPros

@Highwayman I know.

Ok, so the idea is that MS is in the embrace phase with WSL1.

With WSL2, they can start making it "better" for Windows devs so they don't have to use Linux.

After that, Linux support will fall from the record 2%.

CodeLongAndPros

@Highwayman So use a VM or a native Linux install.

Highwayman

@CodeLongAndPros

So use a VM or a native Linux install.

wait what? Where’d that come from? Confused again sorry I’m not very good at following stuff today for whatever reason :(

CodeLongAndPros

@Highwayman Appending it to my last comment

Highwayman

@CodeLongAndPros ah ok I see now ye. *is using iPad* xD

CodeLongAndPros

@Highwayman Do you use Linux?

Highwayman

@CodeLongAndPros hum maybe one day, but right now I don’t have a PC, so I just have my phone and iPad which I can’t really switch the OS on :/

CodeLongAndPros
Highwayman

@CodeLongAndPros it’s a heavily restricted iPhone and iPad. So yes, but actually no. lol. Did you see that article about the ppl who “installed” Linux on a potato?

HahaYes

@CodeLongAndPros No they waiting for r/wallstreetbets

Highwayman

C vs C++

lol.

HahaYes

yayyyy no more star wars puns

CodeLongAndPros

@HahaYes If there is another series, it's Star Trek puns.

HahaYes

@CodeLongAndPros Oh please no they had like 1000 episodes

CodeLongAndPros

@HahaYes The movies I - VI

CodeLongAndPros

@HahaYes Encounter at farpointer

CodeLongAndPros

@HahaYes Would you support a series on Linux programming and tools?

HahaYes
HahaYes

@CodeLongAndPros brb if you made a series about linux programming and tools, you would rule the tutorial section.

CodeLongAndPros

@HahaYes It's decided, then.

HahaYes

@CodeLongAndPros oh okay, see ya