C_96 Passing Array as an Argument to a Function in C | C Language Tutorials

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
so in the series of learning c programming we are discussing functions in c we have discussed some previously gate question paper in the previous videos on functions right and till now we have discussed functions see when we were passing argument in function what we have passed either into variable or float variable or a single character variable that's it right as an argument but what if if you want to pause pass a complete array as an argument string as an argument pointer as an argument to the function so these concepts are left so in this video we'll discuss one concept that is how to pass a complete array as an argument in function that will discuss in this video with a proper program right and why we pass a complete array what is the need of need to pass an area as an argument right with a proper program we will discuss here then i will show you practical loads we will run that program and see what output you will get right and all the important points about this thing like what is the process what is the mechanism behind behind the scene what is happening when you pass an airing that also will discuss one by one all the things about this concept we'll discuss in this video right but before i just want to tell you one thing if you want if you want to have a great career in software development then you must think about the aspects like what's trending in the industry the questions being asked in the interviews how to improve your courtship ranking what is the thought process behind the great application like zometo or laflip card so here an academy wedding is a platform where you can watch weekly shows which you can watch live and the host of these shows are working in some of the top companies like linkedin amazon google and our seven star coders own code chef and industry experts having years of experience so they'll be covering the content which would be really helpful for your career like in the live episodes you can get an opportunity to ask take a charge about the top 20 questions being asked and the you know industry leaders about the recruitment process in top startups and mncs what is their eligibility criteria and how to apply for them and you can also get your resume reviewed by the experts not only this you can also participate into mock interviews and also learn courses on programming languages blockchain and crypto tech aspects of digital marketing data analytics and here is one more show in which rjm will be unlocking one's potential for web development right on from the introduction to building one's career in web development the show would also cover hr tips and tricks during web development interviews the pros and cons of a career in web development and what are some upskilling opportunities one should be taking up throughout their career so the detail of the show and the batch you will find in the link that i'll put in the description box of this video and don't forget to use my code jkl10 to get ten percent ten percent extra discount on your paid subscription so now let's discuss this how to pass an array as an argument i hope you are clear with how to pass a single uh variable either its float or integer or character how to suppose let's take one uh one you know example suppose function is fun and i want to pass only one variable of it is a type so you can simply in declaration you can simply write down the data type in calling you can write down name of the function and here you can pass suppose a variable x or you can pass us into value also both are fine right you can take here in index is equal to 5 or you can pass x in main definition just you have to write in fun and here to receive this you will write data type and name of the variable i hope this is clear to you right but now how to pass but why we pass array see suppose in my class i have 60 students and i want to you know create a function to calculate average of marks of 60 students so i want to pass marks of 60 students in that function a function is like average and i want to pass what marks of 60 students so one method is what either you can pass what 60 variables individual variable is containing marks of one student that is also one way but obviously that is not a good place we are not going to going to pass a 60 values or 60 variables here so rather than this what to store marks of 60 students now same type of data to store same type of data what we use array right so rather than passing 60 variables so 60 values we can simply pass an array like suppose i i am taking in marks and size is 60. here we have 60 marks of 60 students so you can simply pass this array rather than just passing 60 variables or 60 values that is a good idea right so when you want to pass a list of values to a function then we use it right or this is also useful when you you know you want to pause pass a string in function right that is one great application of passing array as an argument that we will discuss in the next video how to pass a string as an argument like a complete name that is character and union the string is nothing but a character array your name is like j jenkins that is what character area or you can say string so how to pass that thing for that we need to pass area as an argument that is one great application of you know passing array so i hope this is clear why we pass carry as an argument now how you will pass array as an argument what you will write here right that we will discuss here with the complete program so let me just write down this program first of all we will declare this function i want to calculate what average of sixty six i'll not take 60 student marks i'll take only five values i'll take a simple example right so here i am taking suppose i am declaring that function int and function name is average return type is in so i want that it should return something right and in declaration what we can do hint that's it why i am writing this let me just show you that the declaration of this average function and here as an argument what i'm accepting i'm not expecting it would pass array this is what for array so here no need to write down the name of that variable whatever you pass so no need to write down the name of the array only you have to tell that this is should be of integer type and this would be array so that is why we are writing these subscript right this is simple declaration and in main in main what you will do i am taking what an array in marks right and i'm taking marks of five students only i'm not taking 60 students so these are marks of 50 students i'm taking in you can take float also it's up to you right now i want to pass these this array so how we will call this function avg name of the function and here what you will pass only the name of this array marks that's it that's it right name of the array nothing else you have to do here right now suppose in the definition what you will do i want that this this should return the average right so it will return some average so here i want to start that i want to you know store that value average in some variable right so i'm taking that variable name is average so before using it you have to declare it a v e r a g you can take it float obviously average would be in float that's up to you i'm just taking in but it should be flow right so it will we are going to accept here the average and just print printf average is percentage d and simply print vote average right now what is the definition how you will define data type name of the function and here what this array it will pass so here what you will write data type int name of the array you can take same name also suppose i am taking marks same name or you can take a b c and a different name also right so here that's it no need to specify the size here this you have to write as an argument when you will pass array as an argument then in definition this would be the case in declaration only write the sorry in function calling only pass the name of the array and this is what the declaration right now here have to calculate first of all sum will do some and then sum divide by total number of students right so here see we don't know how many students are there means we don't know the size of the setting so that also if you want to pass then how you will pass the size of the array see here simply you can write down here suppose you know the size is 5 so simply you can pass five or if you don't know you can calculate the size of the array right how to calculate the size that we will discuss when we do practical of this program right here i am writing only five right when we write down the code on my laptop then we will calculate the size and we will pass that size i'll will take that size in you know a variable name size and then we will pass that variable name here right so now hey 2 argument we are going to pass so here also what in declaration also what you will do one more this is what was obviously a simple variable you need to accept this 5. so here comma end suppose i am taking into a so here also two arguments one is array one is simple end right and semicolon here now how to do sum a for loop for i is equal to zero i less than i less than size of the array size of the arrays in a we have size of the array so i less than a right and i plus plus and here what you will do sum is equal to sum plus name of the array is marks only here marks i i hope you know the formula how to calculate some of the array that's it and after some just calculate what average average same variable name i'm taking here also same also fine here only or you can take different name right so average is equal to what sum divided by total number of student total number of student or you can say the size of arrays in a we have so divided by 5 and now return this thing right so now what you will write after this average i am writing here return average and that's it so now here we are using i sum and average so in this function you have to declare first of all these variables otherwise it will give it so you can declare in i you can declare sum is equal to 0 and you can also declare what you are taking average average also you can take zero three variables right three variables because we are using i'm returning average so it will return here and you can print average here so this is how you will pass array as an argument c now how the salary would be passed first of all when you will call this this thing then control will go here to the main function right so now instead instead memory would be allocated right suppose this is stack i'm taking this a stack right see whenever you run this this program will go into main memory like some coding for that thing memory would be located then for static or global variables then a stack memory then a heap memory right so now in stack what for main memory would be allocated right it's like one frame or it's like one activation record for this function would be l right so now this is for main main function and here we have in main we have what one variable average right we have add a what marks five so here we have some memory how much memory for this array would be allocated five size is five integer is taking four bytes so it means 20 bytes so i'm just drawing this hair 10 15 20 30 and 45 right and here we have what here i'm right taking this name of there is marks name of the array will act as a internal pointer it's a constant internal point and what it will have the base address of the series suppose base addresses hundred next element would be at one zero four one zero eight one one two and one one six integers of four by ten i'm i'm supposing integer is a four byte one thirty two bit and a sixty four bit machine right so now this base address is hundred so hundred would be here so it is acting as an internal pointer to this first element this is what a base address of this area right so marks will contain 100 this is the array this is how memory would be located now next line is this thing this is what calling of the function avg we are passing marks now see whenever you will write down the name of the array what you will think this function call so the control will go here so now this function also memory would be allocated to this function average right and here we have obviously this now you will think this is what another array which is here means these values would be copied 10 15 20 no 20 30 and 45 in this marks right or you can take different name also right so this complete value would be copied here and we have one more variable a in a what would be copied this 5 but this is not the mechanism behind this when you will pass array what it is going to pass the base address of the array so it will pass marks means name of the array in marks we have the base address of the array so it will pass only 100 the base address right so here it will act as this this will act as a pointer considered as a pointer what how the compiler will interpret this line something like this int asterisk marks means it's a pointer name of the pointer is marks and integer type pointer means it is pointing to an integer value i hope you got this not the complete values would be copied so here what would be copied we have one pointer marks and here we have hundred so this marks also pointing to here so this is example of we are passing address so this is example of call by reference not call by value so by default array would be passed this is very important point by call by value method note call by sorry call by reference method not call by value and obviously it's right because suppose array is having marks of 100 students or a list of values are 200 so when you will pass 200 values now now a separate copy would be created here and 200 value would be stored if you pass it call by value that is of no use wastage of memory here also 200 values here also 200 values and again if you call this function again it will be in memory and again 200 values copies would be stored so that is wasted of memory so that is why array would always be passed by call by reference note call by value this is very important point right so it will act as this thing it is not a complete array here it's not a duplicate array right but this is what this 5 we are passing this is what call by value this is what call by reference because this is l right so now here see i is equal to 0 here we have one more variable i that is 0 we have sum also we have average also so here we have sum and average here we have 0 0 in this one so now i hope you know that now the a value is 5 i value 0 i plus plus and sum would be there it will do first of all 0 plus marks of marks of i is 0 means marks of 0 means what it will access this value because obviously this is a pointer to this right so 0 plus 10 is what in sum we have first of all 10 then marks 1 means marks 1 means index is 0 1 2 3 4 means this value would be accessed then 10 plus 15 so this is how total would be done i hope you know the working of this four loop no need to i don't know define you no need to you know dry run this this for loop so i'm just writing down the sum of the array what would be the sum of this array here i think it it would be 120 right and after this for loop average is equal to sum by a sum is equal to 120 divided by a is 5 so 120 divided by 5 is what i think it's 24 so now it will return average average is 24 so now control will be here whatever it will return that will store in average that is 24 and we are going to print here 24. so i hope you got the working have you passed area as an argument and if you want to confirm that here it's not a complete array just a pointer so you can what you can do you can print here size here if you will print size of array using size sizeof operator what you can print a size of array name is just pass header name marks in printf if you will write then it will print here in main it will print 20 bytes because 5 is size 5 into 420 but here if you will write size of marks here in this function it will print 4 byte because it is a pointer it's not a complete array this this thing i'll show you practically so now let me show you practical of this let me just run this code and see what output you are getting so now let me create a file for this and what array as argument right so here what first of all we are declaring name i'm taking app avg right and here hint name is sorry it's just a declaration so no need to specify the name of the array that's it right and in main what we will do is simple take array marks and here i am taking uh sizes 5 and just i'm assigning here values 10 20 30 50 right simple values i am taking right and here i i will pass the size also so here we will calculate the size will not pass 5 only will calculate the size and then will pass so how to calculate size of the series means how many elements in area how to calculate that thing means if you calculate the size of air in bytes like the size is 20 bytes divided by size of a single value like a of 0 that is 20 divided by 4 that is 5 so there are 5 elements right i hope you are getting this method so here simply you can take what i am also taking one variable that is size right and here i am writing size is equal to size of this is the method just pass the name of the array and divide by size of size of his operator size of single value marks and 0 right semicolon so it will give 20 divided by 4 byte [Music] that is 5 and obviously in size we have 5 so there are 5 elements so you can pass this so now average is equal to i'm calling this function average how to call simply pass name of the array and second is size also i want to pass that is the variable name size right so now average obviously average also you have to declare before using it right and size we have declared right now and see yeah in calling we are passing two arguments so here also obviously in declaration also what you have to do one more way ah data type that is int right otherwise it will give error so now after returning just print printf average is equal to percentage d and slash n and simply print the name the variable name where you are storing this average right now what will be the definition name of the function is avg and here i'm taking name of the array is not marks you can take different name also you can take same name also suppose i am taking here name of that is marks one right and here one more hint size you can take same name also size and main size and hair also no need to worry so that that is it and here what now what you will do will take in i then sum and then average right and here you will do some just write down this i less than less than how many elements in array obviously less than size we are going to run and then i plus plus and here what you will do sum is equal to sum plus or you can say sum plus equal to error name is i'm taking here marks one marks one and here i right and after this average is equal to sum divided by size of array that how you calculate average and then return average so here in uh for loop we have semicolon right so now let's see and i know let's run this and see what output you will get okay now see it it was giving a garbage well in the previous case why sometimes it will take garbage value so better to initialize the sum is equal to zero average is equal to zero when you declare these otherwise it will it will take any garbage value and output also will give some garbage value so better to initialize with a zero so now this time it is giving average is 30 so now see here here if you print here in main function here i am printing the size after average i am printing what printf inside main size of array is and i'm i want to calculate size in bytes how many bytes right so percentage d and comma i just write down size of operator size off and pass the name of the array marks right same line what same size i want to print here in average also so before returning i'm pretty inside now it's inside avg function right inside avg function size of arrays in bytes size of i'm taking name of the array here marks one so size of marks one this new to take care right now let me run this and see what output you are getting just for formatting purpose i'm i know writing here slash and i'm putting a slash and here see average is 30 before this it is printing inside average function size of array is 4 only size of array is not 20 it's 4 only why because it is it's not an array copy of the array it's just a pointer marks 1 is just a pointer and pointer is what either four bytes or it depends on the machine right so four bytes or two bytes in 32-bit machine it will take four byte but inside main function size of our is 20 bytes so now i hope you are getting we are not copying the complete array if you copy the complete array in both the function size of array will be 20 and 20 because in main also five elements we have in average function also five elements we have if you have a copy but here we in average we don't have copy it's called by reference it's only point that could be considered as pointer and pointer has having size four bytes right now it's just giving a warning like size of an average integer parameter marks one will return size of int star because it is acting in the compiler interpreted as a pointer and star as a pointer right so here if you will think like like that size rather than calculating size in the main function we simply calculate size in the average function and then we just do i less than equal to size no if you will calculate size here if you will write down the same line here in this average means it will take marks one size of max one would be four and size of marks you know this the single element also four four by four is one so size would be one only so this four would run only one time that is why we calculate size how many elements in array in main function and we pass that value right so that's it for this video now so i hope you got the concept if you have any doubt regarding this you can ask me in comment box right next video we'll see how to pass a string as an argument in adding so in the next video till then bye take care
Info
Channel: Jenny's lectures CS/IT NET&JRF
Views: 7,587
Rating: 4.92278 out of 5
Keywords: data structure tutorials, operating system, data structure and algorithms, jayanti khatri lamba, jennys lectures, jenny data structures, jennys lectures DS, jenny lamba, jennys baby, jennys lectures baby, data structures
Id: 2aScqfAI3oc
Channel Id: undefined
Length: 27min 37sec (1657 seconds)
Published: Sat Sep 18 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.