C Programming Functions PDF

Document Details

Uploaded by Deleted User

Tags

C programming functions C programming functions computer programming

Summary

This document provides a detailed explanation of functions in C programming, including their definition, usage, and classification. It covers crucial aspects such as function prototypes, how they work, different function types, and examples of their implementation.

Full Transcript

A large program in c can be divided to many subprogram The subprogram possesa self contain components and have well define purpose. The subprogram is called as a function Basically a job of function is to do something C program contain at least one function which is main()....

A large program in c can be divided to many subprogram The subprogram possesa self contain components and have well define purpose. The subprogram is called as a function Basically a job of function is to do something C program contain at least one function which is main(). Classification of Function User Librar define y function functio - - n main( printf( ) ) - It is much easier to write a structured program where a large program can be divided into a smaller, simpler task. Allowing the code to be called many times Easier to read and update It is easier to debug a structured program where there error is easy to find and fix 1: #include Arguments/formal  Function names iscube 2: parameter  Variable that are 3: long cube(long requires is long x); 4: Return data  The variable to be 5: long input, type passed on isX(has answer; single arguments)— 6: value can be passed to 7: int function so it can main( void ) 8: perform the specific { task. It iscalled 9: printf(“Enter an integer value: Output ”); Actual parameters 15: }scanf(“%d”, 10: Enter an integer 16: &input); value:4 11: answer 17: long = cube(input); cube(long x) 18: The cube of 4 is64. 19: 12: { long printf(“\ x_cubed; nThe cube of 20: is %ld x_cubed %ld.\n”, =x * x 21: * answer); input, x; return 22: x_cubed; 13: 23: }return 0; 14: Cprogram doesn't execute the statement in function until the function is called. When function iscalled the program can send the function information in the form of one or more argument. When the function is used it is referred to as the called function Functions often use data that is passed to them from the calling function Data ispassed from the calling function to a called function by specifying the variables in a argument list. Argument list cannot be used to send data. Its only copy data/value/variable that pass from the calling function. The called function then performs its operation using the copies. Provides the compiler with the description of functions that will be used later in the program Its define the function before it been used/called Function prototypes need to be written at the beginning of the program. The function prototype must have : A return type indicating the variable that the function will be return Syntax for Function Prototype return-type function_name( arg-type name-1,...,arg-type name-n); Function Prototype Examples It is the actual function that contains the code that will be execute. Should be identical to the function prototype. Syntax of Function Definition return-type function_name( arg-type name-1,...,arg-type name-n) ---- Function heade { declarations; statements; Function return(expressi Body } on); Function Definition Examples float conversion (float celsius) { float fahrenheit; fahrenheit = celcius*33.8 return fahrenheit; } The function name‟s isconversion This function accepts arguments celcius of the type float. The function return a float value. So,when this function iscalled in the program, it will perform its task which isto convert fahrenheit by multiply celcius with 33.8 and return the result of the summation. Can be any of C‟s data type: char int floa t lon g… int func1(...) …… func2(...) Examples: func3(...) Function can be divided into 4 categories:  A function with no arguments and no return value  A function with no arguments and a return value  A function with an argument or arguments and returning no value  A function with arguments and returning a values A function with no arguments and no return value Called function does not have any arguments Not able to get any value from the calling function Not returning any value There is no data transfer between the calling function and called function. #include # include void printline(); void main() { printf("Welcome to function in C"); printline(); printf("Function easy to learn."); printline(); getch(); } void printline() { int i; { printf("-"); } printf("\n"); printf("\ for(i=0;i

Use Quizgecko on...
Browser
Browser