C Functions



Functions are basic blocks of codes , till now we have been using a lot of functions , can you remember , yes one of it is the prints function , before going to discuss function let us see the syntax for a function

Function name ( function parameter )
{
Function body
}

So function name is quite easy to understand , so the function parameter is what we pass into that function , we can say that it is the doorway for a function
Function body is the part where all the action happens

This figure might help to understand this well




 
But programmatically how this work , to see this lets make a simple program that display a message from a function outside main and how it works


function()
{
printf(“hello world”);
}

main()
{
printf(“inside main”);

function();

getch();
}



As we can see we are calling the function “function” inside the main by just referring its name ,  this is how we call a function , we can call as many functions as we want , we can call multiple functions but for that the functions should exist first

One thing to remember is that if a variable is declared inside a function then that variable is just accessible to that function only , in order to make a variable accessible to all the functions we make a global variable , it is nothing but a variable declared at the very above of every function , this example will make this clear


#include<stdio.h>

int total; //global variable
int i;

input() //function
{
printf("Enter a number\n");
scanf("%d",&i); // taking a value and changing the value of i for all function
}

output()
{
total = i *2; // global variable total calculation
}

main() // main fnction
{
input(); // calling input function
 output(); // calling output function
printf("total is %d",total); //displaying the total from main
getch();
}



As we can see , "total" and "I" are global variable and input and output are two user defined functions

We can also pass parameter to a different function and then process the result , this example will make this more clear


int total;

take(int a)
{
total = a * 2;
printf("\n\ndouble is %d",total);
}

main()
{
     
int i;

printf("Enter a number\n");
scanf("%d",&i);

take(i);

getch();
}  


Here we take the variable i in main and pass it to function called take , in take inside its parenthesis we declare a variable of same data type that accepts the value of i , once a=I then everything becomes easy now

It is also possible that a function can return a value to the mother function like

int add(int n1 ,int n2)
{
int result;
result = n1 + n2;
return result;
}

main()
{
Int z;
z = add( 3,5);
printf(“%d”,z);
getch();
}


In this program we first pass value to the add function , the int before the function name signify that this function will return a int value , then the add function accepts the value and returns the result which get stored in the variable called z , and we display the “z” , this is how returning of a value works in a function

No comments:

Post a Comment