Posted By:
11 Jun, 2013 12:16 am PDT

functions in C and C++

how can we use functions in c programming?
Attachment:None


A function is a module or block of program code which deals with a particular task.
Please watch the video for details...

Was it useful?  



1. A functions is a set of instructions written to solve a particular task.

2. Instead of writing the same lines of code repeatedly we can define a function to execute those lines of code and we can call the function whenever needed.

Consider the following:

a.      We have a set of lines of code to perform an action.

int total=10;

printf(“ the sum is=%d”,total);

 

b.      Now to convert this into a function all we need to do is embed them into curl braces with a function name of your choice.

 

Example()

{

int total=10;

printf(“the sum is=%d”,total);

}

 

 

c.      We can also pass some parameters from the main function to the Example() Function:

Example(int parameter1, int parameter2)

{

 int total=10;

printf(“the sum is=%d”,total);

}

 

d.     Now we need to do this – send the output from the Example() function to the main() Function.

Example(int parameter1, int parameter2)

{

int total=10;

printf(“the sum is =%d”,total);

return total;

}

 

e.      Now , embed the function name into the main() function

main()

{

Example();

}

 

f.      That’s it ! Now we can call the Example() function anytime in the program.

 


Was it useful?  




Please login to reply to this problem.



Related Problems on Fixoncloud