Learn to Code via Tutorials on Repl.it!

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

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:

int foo(int x) {
	return ++x;

"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.


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:

def f() {
	int x = 12;
int main() {
	int x = 10;
	printf("%d\n", x);


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:


void plus_seven(int* x) {
	*x += 7;

int main() {
	int x = 0;
	printf("%d\n", x);

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

EpicGamer007 (1765)

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 (1622)

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

CodeLongAndPros (1622)

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

CodeLongAndPros (1622)

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

CodeLongAndPros (1622)

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

CodeLongAndPros (1622)

@AbhayBhat Hrm I feel like Bytewave now...

CodeLongAndPros (1622)

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

EpicGamer007 (1765)

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

CodeLongAndPros (1622)


This is a tutorial about using the stdlibs.

You use this line of code:

`#include <stdio.h>`

and that's it for C stdblibs tutorial.

Don't forget to upvote if you want more people to see this.

And please, sub to my youtube channel! (youtu.be/fakechannel)


Like this?

EpicGamer007 (1765)

@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 (1622)

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

Highwayman (1483)

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 (1622)

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

Highwayman (1483)

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

CodeLongAndPros (1622)

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

Highwayman (1483)

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

CodeLongAndPros (1622)

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

Highwayman (1483)

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

CodeLongAndPros (1622)

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

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

CodeLongAndPros (1622)

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

Highwayman (1483)

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

Edit: you’ve not you’be

CodeLongAndPros (1622)


So do you know what an EEE is?

Highwayman (1483)

@CodeLongAndPros emmm imma say no though maybe so no.

Highwayman (1483)

@CodeLongAndPros honestly f windows I hate them so much sometimes

CodeLongAndPros (1622)

@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 (1622)

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

Highwayman (1483)


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 (1622)

@Highwayman Appending it to my last comment

Highwayman (1483)

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

Highwayman (1483)

@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 :/

Highwayman (1483)

@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 (1861)

@CodeLongAndPros No they waiting for r/wallstreetbets

HahaYes (1861)

yayyyy no more star wars puns

CodeLongAndPros (1622)

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

HahaYes (1861)

@CodeLongAndPros Oh please no they had like 1000 episodes

CodeLongAndPros (1622)

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

HahaYes (1861)

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