Skip to content
Sign upLog in
← Back to Community

C wars: Chapter Six: Return of the function: A Finale

Profile icon
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:

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.

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:

#include<stdio.h> 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:

#include<stdio.h> void plus_seven(int* x) { *x += 7; } int main() { int x = 0; plus_seven(&x): 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

Voters
Profile icon
Inna2020
Profile icon
yfeliipee
Profile icon
mHmdlshfaay2
Profile icon
renaudtp
Profile icon
elcorreodeangel
Profile icon
EpicGamer007
Profile icon
TheForArkLD
Profile icon
Highwayman
Profile icon
lightningrock
Profile icon
CodeLongAndPros
Comments
hotnewtop
Profile icon
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..

Profile icon
CodeLongAndPros

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

Profile icon
EpicGamer007

@CodeLongAndPros
, oh...

Profile icon
CodeLongAndPros

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

Profile icon
CodeLongAndPros

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

Profile icon
EpicGamer007

@CodeLongAndPros
, go for it!

Profile icon
CodeLongAndPros

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

Profile icon
CodeLongAndPros

@AbhayBhat
Hrm I feel like Bytewave now...

Profile icon
CodeLongAndPros

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

Profile icon
EpicGamer007

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

Profile icon
CodeLongAndPros

@AbhayBhat

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) Kthxbai

Like this?

Profile icon
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

Profile icon
CodeLongAndPros

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

Profile icon
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.

Profile icon
CodeLongAndPros

@Highwayman
Updated

Profile icon
Highwayman

@CodeLongAndPros
noice ty 👌

Profile icon
CodeLongAndPros

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

Profile icon
Highwayman

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

Profile icon
CodeLongAndPros

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

Profile icon
Highwayman

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

Profile icon
CodeLongAndPros

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

Profile icon
Highwayman

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

Profile icon
CodeLongAndPros

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

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

Profile icon
Highwayman

@CodeLongAndPros
lol

Profile icon
CodeLongAndPros

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

Profile icon
Highwayman

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

Edit: you’ve not you’be

Profile icon
CodeLongAndPros

@Highwayman

So do you know what an EEE is?

Profile icon
Highwayman

@CodeLongAndPros
emmm imma say no though maybe so no.

Profile icon
CodeLongAndPros

@Highwayman
Wikipedia has a good overview:
https://en.wikipedia.org/wiki/Embrace,_extend,_and_extinguish

Profile icon
Highwayman

@CodeLongAndPros
ahh ok I see.

Profile icon
Highwayman

@CodeLongAndPros
honestly f windows I hate them so much sometimes

Profile icon
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%.

Profile icon
CodeLongAndPros

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

Profile icon
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 :(

Profile icon
CodeLongAndPros

@Highwayman
Appending it to my last comment

Profile icon
Highwayman

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

Profile icon
CodeLongAndPros

@Highwayman
Do you use Linux?

Profile icon
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 :/

Profile icon
CodeLongAndPros

@Highwayman

https://www.linux.com/news/linux-on-iphone-you-can-now-run-postmarketos-on-iphone-7/
image

Profile icon
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?

Profile icon
HahaYes

@CodeLongAndPros
No they waiting for r/wallstreetbets

Profile icon
Highwayman

C vs C++

lol.

Profile icon
HahaYes

yayyyy no more star wars puns

Profile icon
CodeLongAndPros

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

Profile icon
HahaYes

@CodeLongAndPros
Oh please no they had like 1000 episodes

Profile icon
CodeLongAndPros

@HahaYes
The movies I - VI

Profile icon
CodeLongAndPros

@HahaYes
Encounter at farpointer

Profile icon
CodeLongAndPros

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

Profile icon
HahaYes

@CodeLongAndPros
oh okay

Profile icon
HahaYes

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

Profile icon
CodeLongAndPros

@HahaYes
It's decided, then.

Profile icon
HahaYes

@CodeLongAndPros
oh okay, see ya