C Programming Tutorial For Beginners

That value so in a lot of situations you’re just gonna want to give these arrays values up front.

Right off the bat but in a lot of other situations you’re not gonna necessarily know what’s supposed to go in there but in a situation like that you still have to tell see how many elements the array is gonna you still have.

To like rigorously defined like hey this array can only hold ten elements and that’s just so C is able to allocate enough memory in order to hold all of those elements so that’s kind of the basics of working with arrays now I do want to point.

Out one thing that we’ve been using in this course has been strings so I’ve been creating character strings so I could say like char phrase and I could set.

This equal to giraffe Academy or other times in the program we created like when we were making our little game we created an arrays and I would just say like 20 and then I would get input from the user and put that information in here this is basically.

A string of characters so anytime.

We create a string in C like if I was to create a string over here.

I called it like array whatever this is an array so we’ve kind of just been taking this for granted like I’ve just been calling this a string or calling it like a string of characters.

But it’s basically just an array of characters the only difference is this is such a common thing to use in our programs that.

C makes it a little C makes it special and makes it really easy for us to create.

It but just like that array of integers this is also an array so now you kind of have a better understanding of what strings are and what they’re actually doing in this tutorial I’m going.

To talk to you guys about functions in C a function is basically just a collection of code that performs a specific task so what you can do is you can take a.

You know maybe like five or six or twenty lines of code.

And put it inside of a function and then when you want to access that code that’s inside the function you can call the function and generally when you create a function you’ll design the function to perform a specific task so the function will have a specific purpose so I’m gonna show you guys how to create functions how to work with them and we’ll just talk about the basics of.

Using functions like I said a function is basically.

Just a collection of code that does a specific task so we can actually create a function here in our C program now you’ll notice up here we have.

This block of code it says int main and there’s an open and closed parenthesis and it open and close curly brackets and we’ve kind of just been using this for the entire course like up to this point the course we’ve just been kind of.

Using this main little block of code here but actually this is a function and sometimes you’ll hear people will.

Call this a method as well so function and method are two words that essentially mean the same thing but you generally in C we’re gonna refer to these as functions but this is called the main function and the main function is.

Basically a function that’s gonna get executed when we start running our program and so we’ve already been using a function this main function but if you want we can actually create other functions in our program.

So make sure you guys have to do that so let’s go down and we’re gonna go here outside of this function and the way you can tell we’re outside the function is we’re outside of this ending curly bracket so down here I’m going to create a function and when we create a function we actually have to give C a.

Couple pieces of information the first thing we have to tell C is the return type of the function and so actually we’re going.

More about return type in the next tutorial but for now just know that return.

Type is basically the type of data that the function is going to return to the user.

So sometimes your function can actually give information back to whoever calls it in our case though we’re just gonna say void and void basically means that this function isn’t to return any information that’s the first thing that you have to tell see the second thing we have to do is give this function a name and generally when we’re naming.

Want to name the function according to what it does so in our case we’re gonna create a function that says hi to the user so I’m just gonna call this function say hi because that’s what its gonna do it’s gonna say hi whenever I create a function now I want to make an open and closed parenthesis and I’m gonna.

Make an open and closed curly bracket so now any code that.

Goes in between these curly brackets it’s going to be considered code that’s inside of this function so let me show you guys I’m just gonna make this a very simple function we’re gonna do one thing inside of here we’re just gonna make a printf and we’re gonna print.

Out hello user so I’ve created my function it’s called.

Say hi and inside of this function we’re just printing out hello user so now let’s go ahead and run our program and see what happens so I’m just gonna build and run my program but.

You’ll see up here that hello user doesn’t get printed out right in other words I came down here I said printf hello user but this didn’t get printed out and this is the first lesson with functions is.

That in order to use the code inside.

Of the function we have to call.

It calling a function basically means we’re telling C that we want to execute all the code inside of it so if I want to execute this code I can call this function and the way that I do that is just by typing out the.

Name of the function and an open and closed parenthesis so now instead of just printing out nothing C is actually going to go.

Over here and execute all the code in the say hi function so let’s run our code and you’ll see now we’re printing out hello a user so the difference between this main function up here and this say hi function is this main function is a very special function right when we run our program.

This main function basically gets called but then if we want to use other functions we can call them from inside of this main.

Function and that’s sort of how this is gonna work so also I want to show you guys one other thing I just want to illustrate the flow that these functions take I want to show you guys how these get executed by the program so I’m printing out top.

And I’m printing out bottom and I’m calling say hi right here so now I’m gonna run my.

Program again and you’ll see we’re printing out top and then we’re printing out hello user then we’re printing out bottom and this might have looked a little.

Bit better if I put new lines but essentially what’s happening is when C goes to execute this program it’s gonna execute this first line of code printf so it’s.

Gonna print out top then we’re telling C that we want to call this a hi function C is now gonna jump over here to the say hi function and it’s gonna execute all of the code inside of here so in this case we just have one line of code but I could put any.

Lines of code inside this function so if.

I have like five lines of code it would execute all five.

Code then it’s gonna jump back up here and it’s gonna move on to the next instruction so that’s kind of how that works and that’s sort of the basics of writing functions anytime you have a block of code or a bunch of code that does one thing or.

Encapsulate into its own little container that you.

Can call you can put it inside of a function and then whenever you want to access it you.

Just call it but these functions can actually do a lot more so one thing we can do is we can give these functions some information so I can actually give this function a piece of.

Information and it can use that information to do different things this is what’s called a parameter and a parameter is basically just a value that we give to a function so let’s say in this say hi function instead of just saying hi to the user we want.

It to say hi to someone specifically well I can actually come down here.

In these parentheses and I can specify a function parameter and I can specify a parameter a lot like I would specify a variable so I could say for example char name.

And an open and closed square brackets and I’m basically what I’m saying down here is this say hi function is going to take as a parameter in other words as an input a string of characters called name and what I can do is I can come down here and I.

Can actually print that out so I could say percent s and we could print out name now over here when I call this.

Say hi function because I specified that it’s gonna take a parameter.

I have to give it a string so I could give this a string like Mike like I’ll.

Call this say hi function now I’m passing the value Mike into this function so Mike is gonna get stored inside of this name array and.

We’re gonna be able to print it out down here let’s go ahead and run our program and you’ll see it says hello Mike so instead of saying hello user now we’re able to customize what it says the cool thing about this.

Is I could copy this line of code like I could call this function a bunch of times with different.

Names actually I’m gonna put in a new line down here so this is more obvious so I could say hello Mike hello Tom hello Oscar now when I run my program it’s saying hello to.

Each of these different people because we define the function.

And we can pass it different names and depending on what we pass it depending on the parameter that we give it it’ll perform its task a little bit differently you can also specify multiple parameters so let’s say in addition to accepting the name as a parameter we also accepted their age so I.

Could say char name and then down here I can specify int age and now we’re basically saying that this is going to take an age as well so I could say hello and then I could say you are and we’ll say.

The age so now over here instead of an addition to just saying name we can also say age so now when I run this function and when I call it I have to also give this an age so we could say like 40 23 and 70 and now this is gonna print out all that information for us so hello Mike you are 40 hello Tom you’re 23 hello Oscar you’re 70 and you can pass as many parameters as.

You want into one of these functions you always just have to make sure though that you’re passing a parameter when you’re calling the function just like that so that’s the basics of using functions.

And in the next tutorial we’re going to talk about this guy over here which is the return type and we’re gonna look at how we can get information back from a function in this tutorial I’ll talk to you guys about return statements in C.

Where a turn statement is a special line of code that we can include in our functions which will allow them to return information back to whoever called them so I can write a function and that can have that.

Function give information back to whoever called it that could be information like the result of some operation or you know it could be it a message telling whoever.

How the function went it could be anything like that.

So I’m gonna show you guys basically how we can do that and I’ll just give you an overview of like return types and all that stuff so down here I’m actually going to create a function and we’re gonna create a function that cubes a number so when you cube a number generally you take it to the third.

Power so I could say like 2 raised to the third power which is the same as just saying 2 times 2 times 2 this would be cubing a number right you’re taking it to the third power so I’m gonna make a function that’s gonna.

Do that basically this function will accept one parameter which is gonna be a number and it’ll cube that number and then once it’s done cubing the number it’ll give that value back to the caller and you guys will see how that will work so I do want.

Thing whenever we are returning values in our function so if you’re gonna write a function that’s gonna return a value you always want to put it above the function that’s going to be calling it so for example like this main function down here this is the first function that executes so any code we put in here is gonna get executed first if I want to return a value with my function I’m gonna define the function here above the main method and there’s a couple reasons.