Using Functions in C



in this lecture we are

going to introduce using functions in C

I've talked about this a little bit and I mentioned a little bit about what functions are and how to use them .

Syntax of a function

return_type function_name (argument list)

{

    Set of statements – Block of code   

}

format is the function type defines the type of value a function is going to return to the calling function if return type is not mentioned then C will assume it to be of type integer if the function does not return anything then the return type is void function name should be a valid identify a name the name should be appropriate to the task it performs and care should be taken that there are no duplicate names parameter list will receive the data from a calling function they are input data to the function to carry out a task since the represent actual values they are termed as formal parameters the parameters are also known as arguments it consists of declaration of variables separated by commas and surrounded by parentheses .

This is the body of function 

{
    Set of statements – Block of code
}

the body consists of local variable decoration function statements that perform a task return statement if the function does not return any value the return statement can be omitted from the body.

 when we start typing out more code and everything as we move on in this series we're going to start moving more systematically through concepts  let's begin what is a function a function can be used to call or execute multiple statements at one time now the computer actually does go through each statement one at a time but for us as programmers we only have to say one thing so for example if we are trying to print three things to the console our code might look something like this,

printf();

printf();

printf();


now technically you wouldn't have to have three printf statements because you can always break things down to the newline using backslash n but sometimes for readability it's easier to break things up into multiple printf statements that way we don't just have like this huge never-ending string with like a ton of text and a bunch of backslash ends it's harder to consume this with our eyes then smaller printf statements in addition to this we've also created code that is going to monitor some kind of information and we want to continually print out the same statements with new updated information so we might have some kind of sensor that this will print like temperatures or something like that doesn't really matter what we're using this for just make up something in your head essentially we want to execute all three of these numerous times well let they function we could basically group all three of these together  you could execute all three of these at one time  

printf();

printf();

printf();

and give it some kind of name like console dump we're going to dump on all the information straight to the console and then we can just call that using these parentheses that is essentially what a function is it's a shorthand to calling a bunch of statements so this here let's print a statement this is actually a function itself you can see we're calling it with these parentheses or passing in information inside of these parentheses of what we want to print the actual code for the printf statement we don't need to care about that all we need to know is how to use that function who knows how many lines of code that function actually takes up doesn't really matter to us to be honest all we need to know is that when we call this function and pass in some information it goes to the console that's all we care about the 

second benefit of functions 

is that if you want to change something like let's say we have this code in our program ten times and we're not using a function and then we want to go back and change something we're going to have to change it in ten different locations that's not going to work very well if we're using functions we can kind of think about it as extracting this code as a formula and then we only have one copy so this is our individual copy and then we can call it multiple times so now we have two calls and we could repeat this as many times as we wanted the data that we actually pass into a function inside of these parentheses these are known as arguments functions can accept as many arguments as they want the only thing is you have to separate multiple arguments by commas so for example in this printf function we could do something like this 

where int X=5;

printf('' %\n,x);

the name of a variable that source some value the action of putting data here is called passing so if you had to put it into a sentence you would say we are passing two arguments the arguments in this case the first one would be the string here the second one would be X and they're separated by a comma arguments allow us to customize our functions for example in this situation where we had this function we created called console dump or whatever we could accept arguments that allow us to change how the function works you can see that in the print F function the print F function does something different depending upon what we pass in if we're passing in X here and it has the value of 5 that's going to print 5 to the console but if X has the value of 6 it's going to print 6 so if you like this pleas share your friend thanks .


Post a Comment

0 Comments